日期:2014-05-17  浏览次数:20773 次

struts2文件下载中文路径名乱码
现在问题是:英文名称的文件可以下载,中问路径的不可以.代码:
package com.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value="struts-default")
@Namespace(value="/study")
@Result(name="SUCCESS",type="stream",params={"contentType","application/octet-stream;charset=ISO8859-1","inputName","inStream","contentDisposition","attachment;filename=${downloadFileName}","bufferSize","4096"})
public class FileAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String filename;
private InputStream inStream;
@Override
public String execute() throws Exception {
return "SUCCESS";
}

public InputStream getInStream() {
System.out.println("getInStream"+filename);
inStream = ServletActionContext.getServletContext().getResourceAsStream("/store/"+getFilename());
if (inStream == null) {
inStream = new ByteArrayInputStream("Sorry,File not found !"
.getBytes());
}
return inStream;
}
public String getDownloadFileName(){

String downloadileName=filename;
try {
downloadileName=new String(downloadileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("getDownloadFileName:"+downloadileName);
return downloadileName;
}
public void setFilename(String filename) {
try {
this.filename = new String(filename.getBytes(),"ISO8859-1");
System.out.println("setFilename()..........."+filename);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public String getFilename() {
try {
String fn=new String(filename.getBytes(),"ISO8859-1");
System.out.println("getFilename()......"+fn);
return fn;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return this.filename;
}
}
}



------解决方案--------------------
看看我的excel导出代码吧,
Java code

package com.zds.emis.employee.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.zds.emis.employee.service.I