One: The browser opens the file on the server
1: Read the resources above the server, if the Web tier, you can directly use ServletContext, if the non-web layer
You can use the class loader to read files
2: Write data to the browser, actually encapsulate the data on the response object, and then the server discovers the response in response
Data binding in the body and then write to the browser
3: Set the response header, control the browser reading or parsing mode
As follows: Open the picture on the server
1 /**view pictures on the page*/2 Private voidViewimage (httpservletresponse response)throwsIOException {3 //set the response header to view pictures on a Web page4Response.setheader ("Content-type", "Image/jpeg");5InputStream in = This. Getservletcontext (). getResourceAsStream (6"/download/1.jpg");7OutputStream out =Response.getoutputstream ();8 intLength = 0;9 byte[] buf =New byte[1024];Ten while(length = In.read (buf)) > 0) { OneOut.write (buf, 0, length); A } - Out.flush (); - out.close (); the}
Second: Download the file above the server
1: Download the file and open the file is similar to the first read the server above the file, and then want the browser to write files,
Only the response header is different.
Response.setheader ("Content-disposition", "attachment;filename=1.jpg");
1 /**Download Image*/2 Private voidDownloadimage (httpservletresponse response)throwsIOException {3 //set the response header to view pictures on a Web page4Response.setheader ("Content-disposition", "attachment;filename=1.jpg");5InputStream in = This. Getservletcontext (). getResourceAsStream (6"/download/1.jpg");7OutputStream out =Response.getoutputstream ();8 intLength = 0;9 byte[] buf =New byte[1024];Ten while(length = In.read (buf)) > 0) { OneOut.write (buf, 0, length); A } - Out.flush (); - out.close (); the}
2: If you need to get the name of the file, it is best to get the absolute path of the file on the server, then read, write the content to the browser.
String Path = this.getservletcontext (). Getrealpath ("/download/high-round. jpg");
1 Private voidDownloadImage2 (httpservletresponse response) {2String Path = This. Getservletcontext (). Getrealpath ("/download/high round. jpg");3String filename = path.substring (path.lastindexof ("\ \") +1);4 //set the download file response header5Response.setheader ("Content-disposition", "attachment;filename=" +filename);6InputStream in =NULL;7OutputStream out =NULL;8 Try {9in =NewFileInputStream (path);Tenout =Response.getoutputstream (); One intLen = 0; A byte[] buf =New byte[1024]; - while(len = In.read (buf)) > 0){ -Out.write (buf, 0, Len); the } -}Catch(Exception e) { - e.printstacktrace (); -}finally{ + if(NULL!=In ) { - Try { + in.close (); A}Catch(IOException e) { at e.printstacktrace (); - } - } - if(NULL!=Out ) { - Try { - out.close (); in}Catch(IOException e) { - e.printstacktrace (); to } + } - } the}
If the file name is in Chinese, the download will have garbled problems, resulting in the inability to download
At this point we can first encode the file name, as follows:
1 String filename = path.substring (path.lastindexof ("\ \") +1); 2 Try {3 filename = urlencoder.encode (filename, "utf-8"); 4 Catch (unsupportedencodingexception E1) {5 e1.printstacktrace (); 6 }
So the garbled problem is solved!
Javaweb Learning Summary 26 (usage of Response object two download files)