struts2文件下载中文路径名乱码解决方法
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 (Unsupported
EncodingException 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.IEmployeeManageBs;
import com.zds.emis.employee.service.imp.EmployeeManageBs;
import com.zds.emis.employee.vo.EmployeeVo;
public class ExportEmployeeExcel extends HttpServlet {
/**
* Constructor of the object.
*/
public ExportEmployeeExcel() {
super();
}
/**
* Destruction of the servlet. br
*/
public void destroy() {
super.destroy(); // Just puts destroy string in log
// Put your code here
}
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding( gbk );
EmployeeVo employeeVo=new EmployeeVo();
//获取员工编号
employeeVo.setEmpCode(request.getParameter( empCode ));
System.out.println( 你要导出的员工编号为:
+request.getParameter( empCode ));
//获取员工姓名
employeeVo.setEmpName(request.getParameter( empName ));
//初始化Service
IEmployeeManageBs service=new EmployeeManageBs();
//掉用Service
List employeeList=service.queryByCondition(employeeVo);
// 用日期构建输出文件名称
SimpleDateFormat sd = new SimpleDateFormat( yyyy-MM-dd );
String date = sd.format(new Date());
String excelName = 员工信息_ + date + .xls ;
// 解决导出员工数据时汉字乱码问题
excelName = new String(excelName.getBytes(), iso8859-1 );
// 选择地址点击保存或者直接点击查看
response.setContentType( application/vnd.ms-excel );
response.addHeader( Content-Disposition , attachment; filename=\ +
excelName + \ );
OutputStream os = response.getOutputStream();
//利用OutputStream创建WritableWorkbook对象
WritableWorkbook wwb = Workbook.createWorkbook(os);
//设置字体 、颜色 、大小
WritableFont font1=new
WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.GREEN);
WritableFont font2=new
WritableFont(WritableFont.TIMES,18,WritableFont.BOLD);
WritableFont font3=new
WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD);
WritableFont font4=new
WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat format1=new WritableCellFormat(font1);
WritableCellFormat format2=new WritableCellFormat(font2);
WritableCellFormat format3=new WritableCellFormat(font3);
WritableCellFormat format4=new WritableCellFormat(font4);
try {
//把水平对齐方式指定为居中
format1.setAlignment(jxl.format.Alignment.CENTRE);
format2.setAlignment(jxl.format.Alignment.CENTRE);
format3.setAlignment(jxl.format.Alignment.CENTRE);
format4.setAlignment(jxl.format.Alignment.CENTRE);
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 生成名为 员工信息 的工作
,参数0表示这是第一页
WritableSheet sheet=wwb.createSheet( 员工信息 ,0);
//设置单元表格的宽度为:13
sheet.setColumnView(0,13);
sheet.setColumnView(1,13);
sheet.setColumnView(2,13);
sheet.setColumnView(3,13);
sheet.setColumnView(4,15);
sheet.setColumnView(5,15);
sheet.setColumnView(6,15);
sheet.setColumnView(7,13);
sheet.setColumnView(8,13);
sheet.setColumnView(9,13);
try {
//合并单元表格
sheet.mergeCells(0,0,9,0);
//向表格里插入数据 并设置 format属性
//在Label对象的构造之中指名单元格位置是第一列第一行(0,0)
Label label1 = new Label(0, 0, 员工信息表 ,format2);
Label label2 = new Label(0, 1, 工号 ,format4);
Label label3 = new Label(1, 1, 姓名 ,format1);
Label label4 = new Label(2, 1, 性别 ,format1);
Label label14 = new Label(3, 1, 年龄 ,format1);
Label label15 = new Label(4, 1, 电话 ,format1);
Label label16 = new Label(5, 1, 手机 ,format1);
Label label17 = new Label(6, 1, 邮件 ,format1);
Label label18 = new Label(7, 1, 学历 ,format1);
Label label19 = new Label(8, 1, 地址 ,format1);
Label label20 = new Label(9, 1, 状态 ,format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label14);
sheet.addCell(label15);
sheet.addCell(label16);
sheet.addCell(label17);
sheet.addCell(label18);
sheet.addCell(label19);
sheet.addCell(label20);
EmployeeVo[] empVo=new EmployeeVo[employeeList.size()];
for (int i = 0; i employeeList.size(); i++){
empVo[i] =(EmployeeVo) employeeList.get(i);
Label label5 = new Label(0,i+2, +empVo[i].getEmpCode(),format4);
sheet.addCell(label5);
Label label6=new Label(1,i+2, +empVo[i].getEmpName(),format3);
sheet.addCell(label6);
Label label7=new Label(2,i+2, 1 .equals(empVo[i].getEmpSex())? 男 : 女 ,format3);
sheet.addCell(label7);
jxl.write.Number label8=new
jxl.write.Number(3,i+2,empVo[i].getEmpAge(),format3);
sheet.addCell(label8);
Label label9=new Label(4,i+2, +empVo[i].getEmpPhone(),format3);
sheet.addCell(label9);
Label label10=new Label(5,i+2, +empVo[i].getEmpMobile(),format3);
sheet.addCell(label10);
Label label11 = new Label(6,i+2, +empVo[i].getEmpEmail(),format3);
sheet.addCell(label11);
Label label12=new Label(7,i+2, +empVo[i].getEmpDegree(),format3);
sheet.addCell(label12);
Label label13=new Label(8,i+2, +empVo[i].getEmpAddress(),format3);
sheet.addCell(label13);
Label label21=new Label(9,i+2, 1 .equals(empVo[i].getEmpState())? 在
职 : 离职 ,format3);
sheet.addCell(label21);
}
System.out.println( 创建成功 );
// 写入数据并关闭文件
wwb.write();
wwb.close();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* The doPut method of the servlet. br
*
* This method is called when a HTTP put request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
/**
* Initialization of the servlet. br
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}