文件下载

@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" );
 response.setHeader( "Content-Disposition", "attachment;fileName=" + fileName );
 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();
   }
  }
 }
}

猜你喜欢

转载自www.cnblogs.com/libo199374/p/8862512.html