public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = null; // 输出响应正文的输出流
InputStream in = null; // 读取本地文件的输入流
// 获取filename请求参数
String filename = request.getParameter("filename");
String filepath = request.getParameter("filepath");
if (null == filename) {
out = response.getOutputStream();
out.write("Please input filename.".getBytes());
out.close();
return;
}
filename=new String(filename.getBytes("ISO-8859-1"),"UTF-8");
UploadConfiguration cfg = UploadConfiguration.getConfiguration();
String realPath = cfg.getPhysicalPath(filepath);
// 获得读取本地文件的输入流
in = new FileInputStream(new File(realPath));
int length = in.available();
// 设置响应正文的MIME类型
response.setContentType("application/force-download");
response.setHeader("Content-length", String.valueOf(length));
response.setHeader("Content-Disposition", "attachment;filename=\"" + BumfUtils.toUtf8String(filename) + "\" ");
response.setHeader("ContentType","text/html;charset=UTF-8");
// 把本地文件中的数据发送给客户
out = response.getOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[512];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
servlet本地项目下载没问题,部署到服务器的下载文件名乱码
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答
- 小神官 2014-07-28 03:47关注
这个理是util
public static String toUtf8String(String s) { StringBuffer sb = new StringBuffer(); for (int i=0;i<s.length();i++) { char c = s.charAt(i); if (c >= 0 && c <= 255) { sb.append(c); } else { byte[] b; try { b = Character.toString(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; } for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; sb.append("%" + Integer.toHexString(k). toUpperCase()); } } } return sb.toString(); }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)
- ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。