[b][color=red][size=medium]文件下载有两种方法:[/size][/color][/b]
1.直接在网站上放个链接地址,右键另存为,(文件名为中文时下载报错)
2.通过后台实现下载(代码实现 控制中文不乱码)
文件下载代码:
[size=large][i][color=blue][u]response.setContentType 参数参照[/u][/color][/i][/size][url]http://360193550.iteye.com/blog/2002915[/url]
1.直接在网站上放个链接地址,右键另存为,(文件名为中文时下载报错)
2.通过后台实现下载(代码实现 控制中文不乱码)
文件下载代码:
//其中%20是空格在UTF-8下的编码
public String downloadLocal() throws FileNotFoundException, UnsupportedEncodingException {
HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
// 下载本地文件
String fileName = URLEncoder.encode("6767.pdf", "UTF-8"); // 文件的默认保存名
//fileName = fileName.replace("+", "%20");
File file = new File(getPath()+"/upload/ytytry5中文.pdf");
// 读到流中
InputStream inStream = new FileInputStream(getPath()+"/upload/gjw_123456_touzi.pdf");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
[size=large][i][color=blue][u]response.setContentType 参数参照[/u][/color][/i][/size][url]http://360193550.iteye.com/blog/2002915[/url]