java servlet文件下载
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
------解决方案--------------------换一种请求方式。不要直接u.openStream();
改成这样的类型试试
URLConnection conn = url.openConnection();
&nb