下载的ContentType设置

application/octet-stream ---  application/force-download  ---
application/x-download

此报文头content-disposition, 对报文体进行描述, 规定了接收端的显示处理行为。

此报文头的值有两种, attachment 和 inline, 分别表示保存 还是 直接显示。


  1. public void download() {  
  2.         String filePath = this.queueService.getCsvFilePathById(id);//所要下载的文件路径,从数据库中查询得到,当然也可以直接写文件路径,如:C:\\Users\\Administrator\\Desktop\\csv\\号码_utf8_100.csv  
  3.         try {  
  4.             File file = new File(filePath);  
  5.             String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);//得到文件名  
  6.             fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");//把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码,中文不要太多,最多支持17个中文,因为header有150个字节限制。  
  7.             response.setContentType("application/octet-stream");//告诉浏览器输出内容为流  
  8.             response.addHeader("Content-Disposition""attachment;filename="+fileName);//Content-Disposition中指定的类型是文件的扩展名,并且弹出的下载对话框中的文件类型图片是按照文件的扩展名显示的,点保存后,文件以filename的值命名,保存类型以Content中设置的为准。注意:在设置Content-Disposition头字段之前,一定要设置Content-Type头字段。  
  9.             String len = String.valueOf(file.length());  
  10.             response.setHeader("Content-Length", len);//设置内容长度  
  11.             OutputStream out = response.getOutputStream();  
  12.             FileInputStream in = new FileInputStream(file);  
  13.             byte[] b = new byte[1024];  
  14.             int n;  
  15.             while((n=in.read(b))!=-1){  
  16.                 out.write(b, 0, n);  
  17.             }  
  18.             in.close();  
  19.             out.close();  
  20.         } catch (FileNotFoundException e) {  
  21.             e.printStackTrace();  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  

//    public void exportTxt(HttpServletRequest request,HttpServletResponse response,List sucList,List faiList) {
//        String NEW_LINE = System.getProperty("line.separator");
//        try {
//            request.setCharacterEncoding("UTF-8");
//        } catch (UnsupportedEncodingException e1) {
//            e1.printStackTrace();
//        }
//
//        if (faiList.size()>0) {
//           //导出txt文件
//            response.setContentType("application/octet-stream");
//            String fileName="还款失败";
//            try {
//                response.setHeader("Content-Disposition","attachment; filename=" +  java.net.URLEncoder.encode(fileName, "UTF-8")
//                        + ".txt");//导出中文名称
//            } catch (UnsupportedEncodingException e2) {
//                e2.printStackTrace();
//            }
//            BufferedOutputStream buff = null;
//            StringBuffer write = new StringBuffer();
//            ServletOutputStream outSTr = null;
//            try {
//                outSTr = response.getOutputStream();// 建立
//                buff = new BufferedOutputStream(outSTr);
//                String tcb = "\t";
//                write.append("" + tcb);
//                write.append("上传总条数" + sucList.size() + faiList.size()+tcb);
//                write.append("上传成功总条数" + sucList.size()+tcb);
//                write.append("上传失败总条数" + faiList.size()+NEW_LINE);
//                write.append("" + tcb);
//                write.append("上传失败applicationID" +NEW_LINE);
//                for (BuManualRepayment model : faiList) {
//                    write.append(model.getApplicationId()+NEW_LINE);
//                }
//                buff.write(write.toString().getBytes("UTF-8"));
//                buff.flush();
//                buff.close();
//            } catch (IOException e1) {
//                e1.printStackTrace();
//            } finally {
//                try {
//                    buff.close();
//                    outSTr.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//    }

    1.  
    2.     @RequestMapping("file/download")   
    3.     public void fileDownload(HttpServletResponse response){   
    4.         //获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载   
    5.         String path = servletContext.getRealPath("/");   
    6.   
    7.         //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型   
    8.         response.setContentType("multipart/form-data");   
    9.         //2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)   
    10.         response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");   
    11.         ServletOutputStream out;   
    12.         //通过文件路径获得File对象(假如此路径中有一个download.pdf文件)   
    13.         File file = new File(path + "download/" + "download.pdf");   
    14.   
    15.         try {   
    16.             FileInputStream inputStream = new FileInputStream(file);   
    17.   
    18.             //3.通过response获取ServletOutputStream对象(out)   
    19.             out = response.getOutputStream();   
    20.   
    21.             int b = 0;   
    22.             byte[] buffer = new byte[512];   
    23.             while (b != -1){   
    24.                 b = inputStream.read(buffer);   
    25.                 //4.写到输出流(out)中   
    26.                 out.write(buffer,0,b);   
    27.             }   
    28.             inputStream.close();   
    29.             out.close();   
    30.             out.flush();   
    31.   
    32.         } catch (IOException e) {   
    33.             e.printStackTrace();   
    34.         }   
    35.     }   
    36.   
    37.     @Override  
    38.     public void setServletContext(ServletContext servletContext) {   
    39.         this.servletContext = servletContext;   
    40.     }   
    'ez' => 'application/andrew-inset',   
  1.   
  2. 'hqx' => 'application/mac-binhex40',   
  3.   
  4. 'cpt' => 'application/mac-compactpro',   
  5.   
  6. 'doc' => 'application/msword',   
  7.   
  8. 'bin' => 'application/octet-stream',   
  9.   
  10. 'dms' => 'application/octet-stream',   
  11.   
  12. 'lha' => 'application/octet-stream',   
  13.   
  14. 'lzh' => 'application/octet-stream',   
  15.   
  16. 'exe' => 'application/octet-stream',   
  17.   
  18. 'class' => 'application/octet-stream',   
  19.   
  20. 'so' => 'application/octet-stream',   
  21.   
  22. 'dll' => 'application/octet-stream',   
  23.   
  24. 'oda' => 'application/oda',   
  25.   
  26. 'pdf' => 'application/pdf',   
  27.   
  28. 'ai' => 'application/postscript',   
  29.   
  30. 'eps' => 'application/postscript',   
  31.   
  32. 'ps' => 'application/postscript',   
  33.   
  34. 'smi' => 'application/smil',   
  35.   
  36. 'smil' => 'application/smil',   
  37.   
  38. 'mif' => 'application/vnd.mif',   
  39.   
  40. 'xls' => 'application/vnd.ms-excel',   
  41.   
  42. 'ppt' => 'application/vnd.ms-powerpoint',   
  43.   
  44. 'wbxml' => 'application/vnd.wap.wbxml',   
  45.   
  46. 'wmlc' => 'application/vnd.wap.wmlc',   
  47.   
  48. 'wmlsc' => 'application/vnd.wap.wmlscriptc',   
  49.   
  50. 'bcpio' => 'application/x-bcpio',   
  51.   
  52. 'vcd' => 'application/x-cdlink',   
  53.   
  54. 'pgn' => 'application/x-chess-pgn',   
  55.   
  56. 'cpio' => 'application/x-cpio',   
  57.   
  58. 'csh' => 'application/x-csh',   
  59.   
  60. 'dcr' => 'application/x-director',   
  61.   
  62. 'dir' => 'application/x-director',   
  63.   
  64. 'dxr' => 'application/x-director',   
  65.   
  66. 'dvi' => 'application/x-dvi',   
  67.   
  68. 'spl' => 'application/x-futuresplash',   
  69.   
  70. 'gtar' => 'application/x-gtar',   
  71.   
  72. 'hdf' => 'application/x-hdf',   
  73.   
  74. 'js' => 'application/x-javascript',   
  75.   
  76. 'skp' => 'application/x-koan',   
  77.   
  78. 'skd' => 'application/x-koan',   
  79.   
  80. 'skt' => 'application/x-koan',   
  81.   
  82. 'skm' => 'application/x-koan',   
  83.   
  84. 'latex' => 'application/x-latex',   
  85.   
  86. 'nc' => 'application/x-netcdf',   
  87.   
  88. 'cdf' => 'application/x-netcdf',   
  89.   
  90. 'sh' => 'application/x-sh',   
  91.   
  92. 'shar' => 'application/x-shar',   
  93.   
  94. 'swf' => 'application/x-shockwave-flash',   
  95.   
  96. 'sit' => 'application/x-stuffit',   
  97.   
  98. 'sv4cpio' => 'application/x-sv4cpio',   
  99.   
  100. 'sv4crc' => 'application/x-sv4crc',   
  101.   
  102. 'tar' => 'application/x-tar',   
  103.   
  104. 'tcl' => 'application/x-tcl',   
  105.   
  106. 'tex' => 'application/x-tex',   
  107.   
  108. 'texinfo' => 'application/x-texinfo',   
  109.   
  110. 'texi' => 'application/x-texinfo',   
  111.   
  112. 't' => 'application/x-troff',   
  113.   
  114. 'tr' => 'application/x-troff',   
  115.   
  116. 'roff' => 'application/x-troff',   
  117.   
  118. 'man' => 'application/x-troff-man',   
  119.   
  120. 'me' => 'application/x-troff-me',   
  121.   
  122. 'ms' => 'application/x-troff-ms',   
  123.   
  124. 'ustar' => 'application/x-ustar',   
  125.   
  126. 'src' => 'application/x-wais-source',   
  127.   
  128. 'xhtml' => 'application/xhtml+xml',   
  129.   
  130. 'xht' => 'application/xhtml+xml',   
  131.   
  132. 'zip' => 'application/zip',   
  133.   
  134. 'au' => 'audio/basic',   
  135.   
  136. 'snd' => 'audio/basic',   
  137.   
  138. 'mid' => 'audio/midi',   
  139.   
  140. 'midi' => 'audio/midi',   
  141.   
  142. 'kar' => 'audio/midi',   
  143.   
  144. 'mpga' => 'audio/mpeg',   
  145.   
  146. 'mp2' => 'audio/mpeg',   
  147.   
  148. 'mp3' => 'audio/mpeg',   
  149.   
  150. 'aif' => 'audio/x-aiff',   
  151.   
  152. 'aiff' => 'audio/x-aiff',   
  153.   
  154. 'aifc' => 'audio/x-aiff',   
  155.   
  156. 'm3u' => 'audio/x-mpegurl',   
  157.   
  158. 'ram' => 'audio/x-pn-realaudio',   
  159.   
  160. 'rm' => 'audio/x-pn-realaudio',   
  161.   
  162. 'rpm' => 'audio/x-pn-realaudio-plugin',   
  163.   
  164. 'ra' => 'audio/x-realaudio',   
  165.   
  166. 'wav' => 'audio/x-wav',   
  167.   
  168. 'pdb' => 'chemical/x-pdb',   
  169.   
  170. 'xyz' => 'chemical/x-xyz',   
  171.   
  172. 'bmp' => 'image/bmp',   
  173.   
  174. 'gif' => 'image/gif',   
  175.   
  176. 'ief' => 'image/ief',   
  177.   
  178. 'jpeg' => 'image/jpeg',   
  179.   
  180. 'jpg' => 'image/jpeg',   
  181.   
  182. 'jpe' => 'image/jpeg',   
  183.   
  184. 'png' => 'image/png',   
  185.   
  186. 'tiff' => 'image/tiff',   
  187.   
  188. 'tif' => 'image/tiff',   
  189.   
  190. 'djvu' => 'image/vnd.djvu',   
  191.   
  192. 'djv' => 'image/vnd.djvu',   
  193.   
  194. 'wbmp' => 'image/vnd.wap.wbmp',   
  195.   
  196. 'ras' => 'image/x-cmu-raster',   
  197.   
  198. 'pnm' => 'image/x-portable-anymap',   
  199.   
  200. 'pbm' => 'image/x-portable-bitmap',   
  201.   
  202. 'pgm' => 'image/x-portable-graymap',   
  203.   
  204. 'ppm' => 'image/x-portable-pixmap',   
  205.   
  206. 'rgb' => 'image/x-rgb',   
  207.   
  208. 'xbm' => 'image/x-xbitmap',   
  209.   
  210. 'xpm' => 'image/x-xpixmap',   
  211.   
  212. 'xwd' => 'image/x-xwindowdump',   
  213.   
  214. 'igs' => 'model/iges',   
  215.   
  216. 'iges' => 'model/iges',   
  217.   
  218. 'msh' => 'model/mesh',   
  219.   
  220. 'mesh' => 'model/mesh',   
  221.   
  222. 'silo' => 'model/mesh',   
  223.   
  224. 'wrl' => 'model/vrml',   
  225.   
  226. 'vrml' => 'model/vrml',   
  227.   
  228. 'css' => 'text/css',   
  229.   
  230. 'html' => 'text/html',   
  231.   
  232. 'htm' => 'text/html',   
  233.   
  234. 'asc' => 'text/plain',   
  235.   
  236. 'txt' => 'text/plain',   
  237.   
  238. 'rtx' => 'text/richtext',   
  239.   
  240. 'rtf' => 'text/rtf',   
  241.   
  242. 'sgml' => 'text/sgml',   
  243.   
  244. 'sgm' => 'text/sgml',   
  245.   
  246. 'tsv' => 'text/tab-separated-values',   
  247.   
  248. 'wml' => 'text/vnd.wap.wml',   
  249.   
  250. 'wmls' => 'text/vnd.wap.wmlscript',   
  251.   
  252. 'etx' => 'text/x-setext',   
  253.   
  254. 'xsl' => 'text/xml',   
  255.   
  256. 'xml' => 'text/xml',   
  257.   
  258. 'mpeg' => 'video/mpeg',   
  259.   
  260. 'mpg' => 'video/mpeg',   
  261.   
  262. 'mpe' => 'video/mpeg',   
  263.   
  264. 'qt' => 'video/quicktime',   
  265.   
  266. 'mov' => 'video/quicktime',   
  267.   
  268. 'mxu' => 'video/vnd.mpegurl',   
  269.   
  270. 'avi' => 'video/x-msvideo',   
  271.   
  272. 'movie' => 'video/x-sgi-movie',   
  273.   
  274. 'ice' => 'x-conference/x-cooltalk',

你可能感兴趣的:(Java,se)