JSF 文件下载不提示文件对话框

JSF 文件下载不提示文件对话框,第1张

下载不能用<a4j:commandLink value="文件下载"

actionListener="#{productFormBean.downloadFile}">

请用<h:

问题:你需要清空并且关闭流对象。

我提供两种解决方案,并且解决了中文文件名乱码问题,兼容IE和火狐两种浏览器。

你可以选择。

方案一:代码写在一个.jsp文件中。

下载请求页面传递过来的参数(当然具体看你要几个参数)

<rich:column>

<h:outputLink value="file.jsp" rendered="true">

<f:param name="docId" value="#{item.id.docId}" / >

<f:param name="revNo" value="#{item.id.revNo}" />

<f:param name="attachmentNo" value="#{item.id.attachmentNo}" />

<f:param name="filename" value="#{item.fileName}" />

<f:param name="storageName" value="#{item.storageName}" />

<h:outputText value="#{item.fileName}" />

</h:outputLink>

</rich:column>

file.jsp 文件代码:

<%@page language="java" contentType="application/octet-stream"pageEncoding="utf-8"%>

<%@page import="java.io.*,java.util.*,java.net.URLEncoder"%>

<%response.reset()

String ATTACHMENTSTOREPATH = 已经上传了的附件的路径,自己应该知道?

int docId = 0

int revNo = -1

int attachmentNo = 0

String filename = null

String storageName = null

try {

docId = Integer.parseInt(request.getParameter("docId"))

revNo = Integer.parseInt(request.getParameter("revNo"))

attachmentNo = Integer.parseInt(request.getParameter("attachmentNo"))

//在火狐浏览器下载,含空格的文件名会出现异常,于是将空格用下划线代替

filename = (new String(request.getParameter("filename").

getBytes("ISO-8859-1"),"UTF-8")).replace(" ", "_")

//将空格转化为下划线后重新对文件名进行UTF-8编码

filename = java.net.URLEncoder.encode(filename, "UTF-8")storageName = request.getParameter("storageName")

} catch (NumberFormatException nfe) {

}

if (docId >= 0 &&revNo >-1 &&attachmentNo >= 0) {

String filePath = null

filePath = ATTACHMENTSTOREPATH + File.separator + storageName

response.setContentType("application/octet-stream")

//因为浏览器会将字符GBK编码,所以从数据库获得的UTF-8需要转换成GBK

//UTF-8一个汉字24位,GBK一个汉字16位

//Start UTF-8 to GBK(相对以前的程序主要就是修改了这里)

String str=filename

StringBuffer sb = new StringBuffer()

for(int i=0i<str.length()i++) {

char c = str.charAt(i)

switch (c) {

case '+':

sb.append(' ')

break

case '%':

try {

sb.append((char)Integer.parseInt(

str.substring(i+1,i+3),16))

}

catch (NumberFormatException e) {

throw new IllegalArgumentException()

}

i += 2

break

default:

sb.append(c)

break

}

}

String result = sb.toString()

result= new String(result.getBytes("ISO-8859-1"),"UTF-8")

response.addHeader("Content-Disposition", "attachmentfilename=" + new String(result.getBytes("GBK"),"ISO-8859-1"))

//End UTF-8 to GBK

BufferedOutputStream bos = null

BufferedInputStream bis = null

try {

bos = new BufferedOutputStream(response.getOutputStream())

bis = new BufferedInputStream(new FileInputStream(filePath))

byte[] buffer = new byte[1024]

int n = -1

while ((n = bis.read(buffer)) >-1) {

bos.write(buffer, 0, n)

}

} catch (Exception e) {

e.printStackTrace()

} finally {

if (bis != null)

bis.close()

if (bos != null){

out.clear() out = pageContext.pushBody() bos.close() } bis = null

bos = null

}

}

%>

此方案中如果你去掉:out.clear()

out = pageContext.pushBody()

报的错将和你的一模一样,不信你试试?

方案二:用Servlet下载,和你的是一样的。

还是页面过来的参数: <h:outputLink value="/***/***">其中/***/***是你调用的Servlet

<rich:column>

<h:outputLink value="/***/***">

<f:param name="storage" value="#{list.storageName}" />

<f:param name="display" value="#{list.displayName}" />

<f:param name="status" value="#{list.status}" />

<h:outputText styleClass="input1_9pt_ul" value="#{list.displayName}" />

</h:outputLink>

</rich:column>

public class PrFileServlet extends HttpServlet {

//stauts判断使用哪个路径

private String UPLOAD_TEMP_PATH = "临时路径(还没有保存)"

private String UPLOAD_PATH = "文件路径(已经保存了文件)"

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException{

String storageName =

(request.getParameter("storage")==null?"":request.getParameter("storage"))

//在火狐浏览器下载,含空格的文件名会出现异常,于是将空格用下划线代替

String displayName =new String(request.getParameter("display").getBytes("ISO-8859-1"),"UTF8").replace(" ", "_")

//将空格转化为下划线后重新对文件名进行UTF-8编码

displayName = java.net.URLEncoder.encode(displayName, "UTF-8")

String status = (request.getParameter("status")==null?"":request.getParameter("status"))

String code = (request.getParameter("code")==null?"":request.getParameter("code"))

String filePath = ""

if (status.equals("N")) {

filePath = UPLOAD_PATH + "//" + storageName.substring(0, 6)

} else if (status.equals("T")) {

filePath = UPLOAD_TEMP_PATH

}

File file = new File(filePath, storageName)

if (file == null) {

response.sendRedirect("/weberp/workflow/pr/fileNotFound.jsp")

return

}

if (!file.exists()) {

response.sendRedirect("/weberp/workflow/pr/fileNotFound.jsp")

return

}

String contentType = getServletContext().getMimeType(storageName)

if (contentType == null) {

contentType = "application/octet-stream"

}

BufferedInputStream input = null

BufferedOutputStream output = null

try {

input = new BufferedInputStream(new FileInputStream(file))

int contentLength = input.available()

// Init servlet response.

response.reset()

response.setContentType(contentType)

response.setContentLength(contentLength)

//因为浏览器会将字符GBK编码,所以从数据库获得的UTF-8需要转换成GBK

//UTF-8一个汉字24位,GBK一个汉字16位

//Start UTF-8 to GBK(相对以前的程序主要就是修改了这里)

String str=displayName

StringBuffer sb = new StringBuffer()

for(int i=0i<str.length()i++) {

char c = str.charAt(i)

switch (c) {

case '+':

sb.append(' ')

break

case '%':

try {

sb.append((char)Integer.parseInt(

str.substring(i+1,i+3),16))

}

catch (NumberFormatException e) {

throw new IllegalArgumentException()

}

i += 2

break

default:

sb.append(c)

break

}

}

String result = sb.toString()

result= new String(result.getBytes("ISO-8859-1"),"UTF-8")

response.setHeader("Content-Disposition", "attachmentfilename=" + new String(result.getBytes("GBK"),"ISO-8859-1"))

//End UTF-8 to GBK

output = new BufferedOutputStream(response.getOutputStream())

// Write file contents to response.

for (int data(data = input.read()) != -1) {

output.write(data)

}

// Finalize task.

output.flush()

} catch (IOException e) {

// Something went wrong?

e.printStackTrace()

} finally {

// Gently close streams.

close(output)

close(input)

}

}

private static void close(Closeable resource) {

if (resource != null) {

try {

resource.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

好好看懂,要有耐心。希望对你有所帮助。

不会吧?我做的下载页面虽然不是纯JSF的,传递参数是用的JSF,而且解决了中文乱码问题,包括文件名,文件内容。希望对你有帮助。

下载请求页面传递过来的参数:

<rich:column>

<h:outputLink value="file.jsp" rendered="true">

<f:param name="docId" value="#{item.id.docId}" / <f:param name="revNo" value="#{item.id.revNo}" />

<f:param name="attachmentNo" value="#{item.id.attachmentNo}" />

<f:param name="filename" value="#{item.fileName}" />

<f:param name="storageName" value="#{item.storageName}" />

<h:outputText value="#{item.fileName}" />

</h:outputLink>

</rich:column>

执行请求的file.jsp 下载页面

<%@page language="java" contentType="application/octet-stream" pageEncoding="utf-8"%>

<%@page import="java.io.*,java.util.*,java.net.URLEncoder"%>

<%response.reset()

String ATTACHMENTSTOREPATH = File.separator + "local"

+ File.separator + "uploads" + File.separator + "wf"

+ File.separator + "cpi"//此处以CPI为例

int docId = 0

int revNo = -1

int attachmentNo = 0

String filename = null

String storageName = null

try {

docId = Integer.parseInt(request.getParameter("docId"))

revNo = Integer.parseInt(request.getParameter("revNo"))

attachmentNo = Integer.parseInt(request.getParameter("attachmentNo"))

//在火狐浏览器下载,含空格的文件名会出现异常,于是将空格用下划线代替

filename = (new String(request.getParameter("filename").

getBytes("ISO-8859-1"),"UTF-8")).replace(" ", "_")

//将空格转化为下划线后重新对文件名进行UTF-8编码

filename = java.net.URLEncoder.encode(filename, "UTF-8") storageName = request.getParameter("storageName")

} catch (NumberFormatException nfe) {

}

if (docId >= 0 &&revNo >-1 &&attachmentNo >= 0) {

String filePath = null

filePath = ATTACHMENTSTOREPATH + File.separator + storageName

response.setContentType("application/octet-stream")

//因为浏览器会将字符GBK编码,所以从数据库获得的UTF-8需要转换成GBK

//UTF-8一个汉字24位,GBK一个汉字16位

//Start UTF-8 to GBK(相对以前的程序主要就是修改了这里)

String str=filename

StringBuffer sb = new StringBuffer()

for(int i=0i<str.length()i++) {

char c = str.charAt(i)

switch (c) {

case '+':

sb.append(' ')

break

case '%':

try {

sb.append((char)Integer.parseInt(

str.substring(i+1,i+3),16))

}

catch (NumberFormatException e) {

throw new IllegalArgumentException()

}

i += 2

break

default:

sb.append(c)

break

}

}

String result = sb.toString()

result= new String(result.getBytes("ISO-8859-1"),"UTF-8")

response.addHeader("Content-Disposition", "attachmentfilename=" + new String(result.getBytes("GBK"),"ISO-8859-1"))

//End UTF-8 to GBK

BufferedOutputStream bos = null

BufferedInputStream bis = null

try {

bos = new BufferedOutputStream(response.getOutputStream())

bis = new BufferedInputStream(new FileInputStream(filePath))

byte[] buffer = new byte[1024]

int n = -1

while ((n = bis.read(buffer)) >-1) {

bos.write(buffer, 0, n)

}

} catch (Exception e) {

e.printStackTrace()

} finally {

if (bis != null)

bis.close()

if (bos != null)

bos.close()

bis = null

bos = null

}

}

%>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/7991347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存