@RequestMapping( value = "/download.shtml", method = { RequestMethod.GET, RequestMethod.POST } )
public void downloadFile( HttpServletResponse response )
{
File file = new File( wjlj );
String fileName = file.getName();
response.setCharacterEncoding( "utf-8" ); //设置编码格式
response.setContentType( "multipart/form-data" ); //设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setHeader( "Content-Disposition", "attachment;fileName=" + fileName ); // 2.设置文件头:最后一个参数是设置下载文件名
//(这里 可以设置成excel格式 :response.setHeader("Content-Disposition", "attachment;fileName=" + “文件名” + ".xsl");
//可以设置成.pdf格式 :response.setHeader("Content-Disposition", "attachment;fileName=" + “文件名” + ".pdf");
InputStream inputStream = null;
try {
inputStream = new FileInputStream( file );
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ( (length = inputStream.read( b ) ) > 0 )
{
os.write( b, 0, length );
}
os.close();
} catch ( Exception e ) {
e.printStackTrace();
} finally {
if ( inputStream != null )
{
try {
inputStream.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}