strut2下载文件
public String downFruit(){
Map<String, String> map = iProductServ.downFruitPath(fruitid);
HttpServletResponse resp = ServletActionContext.getResponse();
OutputStream out;//输出响应正文的输出流
InputStream in;//读取本地文件的输入流
//获得本地输入流
File file = new File(map.get("fruitPath")); //下载文件路径
try {
in = new FileInputStream(file);
//设置响应正文的MIME类型
resp.setContentType("application/octet-stream;charset=utf-8"); //二进制流下载,可下载任意类型文件
resp.setHeader("Content-Disposition", "attachment;" + " filename="+ new String(map.get("fruitName").getBytes(), "ISO8859-1")); //下载we的文件名
//把本地文件发送给客户端
out = resp.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[512];
while((byteRead = in.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
in.close();
out.close();
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return null;//返回null
}
916

被折叠的 条评论
为什么被折叠?



