Preface
Local download files have actually been contacted before, but there is always the question is that the download must open a new window, through the AJAX request can not achieve the purpose of downloading files, so generally for the exception to capture my custom reaction in the interface I usually write another interface, take into account all kinds of exceptions, and then execute the new download command. If there is a better way to tell me, this article is mainly about downloading files in two ways and the picture in the browser. Of course, these two implementations are inseparable from the response settings. content Show pictures by browser
At present, the company has this demand, the picture is downloaded to the local, but not on the image server, also no longer the root of the project, how to find the picture, and the picture into the IMG tag, this way can be achieved
@ResponseBody @RequestMapping (value = "/test.action") public void Base64tofile (String base64, String fileName,
HttpServletResponse response) {file file = null;
InputStream FIS = null;
OutputStream fos = null; try {file = new file ("G:/image/2.jpg")//assumes the path of the picture FIS = new Bufferedinputstream (New Fileinputstre
AM (file));
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Sets the header Response.reset () of the response;
Response.setheader ("Content-type", "image/jpg");
Response.AddHeader ("Content-length", "" "+ File.length ());
FOS = new Bufferedoutputstream (Response.getoutputstream ());
Fos.write (buffer);
Response.flushbuffer ();
catch (Exception e) {e.printstacktrace ();
finally {if (FIS!= null) {try {fis.close ();catch (IOException e) {e.printstacktrace ();
} if (fos!= null) {try {fos.close ();
catch (Exception E2) {e2.printstacktrace (); }
}
}
}
The pictures in the browser are shown below
Download Files
1. This is my first contact in the way, where the use of Httpheaders and Httpstatus are included in the Springmvc class, are some of the encapsulated enumeration class, if you want to know more about these things can go online reference blog
@RequestMapping (value = "/downfile.action") public responseentity<byte[]> Downfile () {httpheaders Heade
rs = new httpheaders ();
InputStream FIS = null;
byte[] buffer = NULL;
try {String filepath = "g:/image/2.jpg";
File File = new file (filepath);
String filename = File.getname ();
Headers.setcontentdispositionformdata ("attachment", filename);
FIS = new Bufferedinputstream (new FileInputStream (file));
Buffer= new Byte[fis.available ()];
Fis.read (buffer);
return new responseentity<byte[]> (Buffer,headers,httpstatus.ok);
catch (Exception e) {e.printstacktrace ();
return new responseentity<byte[]> (Buffer,headers,httpstatus.not_found);
finally {if (FIS!= null) {try {fis.close (); catch (IOException e) {E.printstackTrace (); }
}
}
}
Summary
In fact, when we understand these things, we can quickly give a solution to the abnormal situation of the browser, to speed up the development of Web pages is very helpful, life-long learning.