struts2文件下载中文乱码解决方案

最近做一个项目,是批量上传附件的功能,一个文档下上传多个附件,然后在查看的时候可以显示文档包含的附件,并且点击文件名可以查看下载附件。因为上传的附件名称可以是中文,所以按照常规的下载方式是不行的,会出现乱码而找不到文件导致报错。

在网上度了关于struts2文件下载中文乱码的解决方案,我参照我做了一次,以下是页面,action以及struts.xml的代码:
jsp页面:
<tr>
<td>
附件:
<font size=2 color="red">
<s:iterator value="#request.files" id="item" status="stus">
<a href="downFile.action?filename=${IMGNAME }&inputpath=${IMGPATH}">${IMGNAME }   </a>
</s:iterator>
</font>
</td>
</tr>

action的写法:
public class downloadFile implements Action {

private String filename;// 初始的通过param指定的文件名属性
private String inputpath; //初始化文件的绝对路径
private InputStream inputStream; //注:我发现网上很多下载的action是没有这个属性的,我自己测试的时候发现没有的话就会出现一个 not found inputStream 的异常,这个在struts.xml的配置里面有。
public InputStream getInputStream() throws Exception {

// 通过 ServletContext,也就是application 来读取数据

return ServletActionContext.getServletContext().getResourceAsStream(inputPath);

}

public String download() throws Exception {

return SUCCESS;

}

public void setInputPath(String value) {

inputPath = value;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

/** 提供转换编码后的供下载用的文件名 */

public String getDownloadFileName() {

String downFileName = fileName;

try {

downFileName = new String(downFileName.getBytes(), "ISO8859-1");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return downFileName;

}

}


struts.xml文件

<action name="download" class="com.downloadFile">
<result name="success" type="stream">

<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!--如果没有在action中填写inputStream的话,这里就会出异常-->
<param name="inputName">inputStream</param>

<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性

对应action类中的方法 getDownloadFileName() -->

<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>

<param name="bufferSize">4096</param>

</result>
<action>
参考于:http://pengranxiang.iteye.com/blog/259401

按照上面的配置我植入到我自己的项目中结果还是不能解决中文附件名的乱码问题,只要是中文名的附件,就会出现乱码。头疼了很久,最后想了一个投机的方法,既然我在数据库中存入的不是乱码,那么我需要的数据我直接从库里面读出来就可以了
于是我在页面上传值的时候直接传一个附件的ID过去,然后在从库中读出我想要的信息,这样就轻松的避免了中文乱码的问题。
具体代码如下:
jsp页面:
<tr>
<td>
附件:
font size=2 color="red">
<s:iterator value="#request.files" id="item" status="stus">
<a href="downFile.action?id=${ID }">${IMGNAME }   </a>
</s:iterator>
</font>
</td>
</tr>

我的action:
package com.wendang.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wendang.manager.IuploadSimpleManager;
import com.wendang.model.Img;

public class downAction extends ActionSupport {

private int id;
private IuploadSimpleManager uploadSimpleManager;

public IuploadSimpleManager getUploadSimpleManager() {
return uploadSimpleManager;
}

public void setUploadSimpleManager(IuploadSimpleManager uploadSimpleManager) {
this.uploadSimpleManager = uploadSimpleManager;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public void downFile(){
int id=this.getId();
String filename="";
Img file=uploadSimpleManager.find_byId(id);
try {
InputStream in=new FileInputStream(file.getIMGPATH());
try {
filename=new String(file.getIMGNAME().getBytes("GBK"), "ISO8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-download");// 设置为下载application/x-download
response.addHeader("content-type ", "application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentType("application/octet-stream");
//FileInputStream fis = null;
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
//fis = new FileInputStream(file);
input = new BufferedInputStream(in);
output = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[4096];
int n = 0;
while ((n = input.read(buff)) != -1) {
output.write(buff, 0, n);
}
output.flush();
} catch (Exception e) {
} finally {
try {
if (output != null) {
output.close();
output = null;
}
if (input != null) {
input.close();
input = null;
}
if (in != null) {
in.close();
in = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

struts.xml配置超简单:

<!-- 下载文件的Action定义 -->
<action name="downFile" class="com.wendang.action.downAction" method="downFile">
</action>



以上就是我解决下载中文乱码的方法,关于struts2的自带配置的下载方案,我按照网上大同小异的方法用在我的项目里的时候总是无法解决,看来我还得好好研究下,那位朋友要是也遇到我这样的问题,不妨换一下这种思路做一下,能解决燃眉之急就好!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值