java servlet文件下载

非我不可道 2013-10-18 06:09:15
servlet 下载zul格式的远程文件 出现IO异常

servlet代码:

1. try{
2. String fileName = request.getParameter("filename");
3. log.info("下载文件名:"+fileName);
4. String url = request.getParameter("url");
5. if( fileName != null && from_charset!=null && to_charset!=null){
6. fileName = new String(fileName.getBytes(from_charset), 7. to_charset);
8. log.info("编码转换后的下载文件名:"+fileName);
9. }
10. log.info("下载文件url:"+url);
11. URL u = new URL(url);
12. BufferedInputStream bis = null;
13. try{
14. bis = new BufferedInputStream(u.openStream());
15. response.setContentType("application/x-msdownload");
16. response.setHeader("Content-Disposition", "attachment;filename=" 17. + java.net.URLEncoder.encode(fileName, "UTF-8"));
18. OutputStream out = response.getOutputStream();
19. byte[] buffer = new byte[1024];
20. int length = 0;
21. while( (length = bis.read(buffer))!=-1 ){
22. out.write(buffer, 0, length);
23. }
24. }catch (FileNotFoundException e) {
25. // TODO: handle exception
26. log.error("文件不存在",e);
27. }catch (IOException e) {
28. // TODO: handle exception
29. log.error("读取文件失败",e);
30. }finally{
31. if( bis != null ) bis.close();
32. }
33. }catch(Exception e){
34. log.error("文件下载执行异常:",e);
35. }


url ="http://98.15.150.28:8081/20131013/1/4872e26ce5f7472429b787bde2029a82.rar"
fileName = "a-ifs.rar";
参数等于这个时候,文件可以正常下载
但是,当下载的是zul格式的文件的时候,代码运行到第14行报错。
参数:
url ="http://98.15.150.28:8081/20131013/1/4872e26ce5f7472429b787bde20123.zul"
fileName = "a-ifs2.rar";
异常:
java.io.ioexception: server returned http response: 500 for url: http://98.15.150.28:8081/20131013/1/4872e26ce5f7472429b787bde20123.zul


...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落夏天 2013-10-18
  • 打赏
  • 举报
回复
换一种请求方式。不要直接u.openStream(); 改成这样的类型试试 URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(1000 * 5); conn.getIntputStream();

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧