java: copy file and write into response

copy file, write the file into response:
response.setContentType( “APPLICATION/x-msdownload”);
response.addHeader(“Content-Disposition”, “attachment;filename=\””
                   + new String((ca.getFilename()).getBytes(“GBK” ),
                   “ISO8859_1”) + “\”” );
OutputStream out = null;
out = response.getOutputStream();
FileInputStream fio = new FileInputStream(file);
 try {
     int length=2097152;
      byte[] buffer=new byte[length];
     int inc = -1;
     while ((inc = fio.read(buffer)) != -1) {
         out.write(buffer, 0, inc);
     }
     out.flush();
} catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
} finally {
     fio.close();
     out.close();
}
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s