- OutputStream out=response.getOutputStream();
- byte by[]=new byte[500];
- File fileLoad=new File(path);
- response.reset();
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("content-disposition","attachment; filename=text.xls");
- long fileLength=fileLoad.length();
- String length1=String.valueOf(fileLength);
- response.setHeader("Content_Length",length1);
- FileInputStream in=new FileInputStream(fileLoad);
- int n;
- while((n=in.read(by))!=-1){
- out.write(by,0,n);
- }
- in.close();
- out.flush();
以上代码以下载excel为例,各种文件的下载大体相同只需改变相应的contentType即可。
其中response.setContentType()的String参数及对应类型为
Java代码 BMP
GIF JPEG TIFF DCX PCX HTML TXT XML AFP PDF RTF MSWORD MSEXCEL MSPOWERPOINT WORDPERFECT WORDPRO VISIO FRAMEMAKER LOTUS123" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <option value="image/bmp">BMP</option>
- <option value="image/gif">GIF</option>
- <option value="image/jpeg">JPEG</option>
- <option value="image/tiff">TIFF</option>
- <option value="image/x-dcx">DCX</option>
- <option value="image/x-pcx">PCX</option>
- <option value="text/html">HTML</option>
- <option value="text/plain">TXT</option>
- <option value="text/xml">XML</option>
- <option value="application/afp">AFP</option>
- <option value="application/pdf">PDF</option>
- <option value="application/rtf">RTF</option>
- <option value="application/msword">MSWORD</option>
- <option value="application/vnd.ms-excel">MSEXCEL</option>
- <option value="application/vnd.ms-powerpoint">MSPOWERPOINT</option>
- <option value="application/wordperfect5.1">WORDPERFECT</option>
- <option value="application/vnd.lotus-wordpro">WORDPRO</option>
- <option value="application/vnd.visio">VISIO</option>
- <option value="application/vnd.framemaker">FRAMEMAKER</option>
- <option value="application/vnd.lotus-1-2-3">LOTUS123</option>