This example for you to share the Java server to download the image of the method for your reference, the specific content as follows
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.URL;
Import java.net.URLConnection;
Import Org.apache.commons.io.IOUtils; /** * Download pictures from server * * @param fileName Image Address * @param response * @return * * @RequestMapping (value = "/down Load ") public void Downloadmedia (httpservletresponse response, HttpServletRequest request) {InputStream InputStream
= NULL;
OutputStream outputstream = null;
try {//handle Chinese garbled request.setcharacterencoding ("UTF-8");
String fileName = request.getparameter ("filename");
FileName = new String (filename.getbytes ("iso8859-1"), "UTF-8"); Handles browser-compatible Response.setcontenttype ("Application/msexcel;charset=utf-8");//Define Output Type Enumeration enumeration = Requ
Est.getheaders ("User-agent");
String browsername = (string) enumeration.nextelement ();
Boolean Ismsie = Browsername.contains ("MSIE"); if (Ismsie) {Response.AddHeader ("CoNtent-disposition "," attachment;filename= "+ urlencoder.encode (fileName," UTF8 ")); else {Response.AddHeader ("content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312")
, "iso8859-1")); //url address If there is a space, will cause an error!
The workaround is: replace the space in the URL parameter with either + or%20.
FileName = Filename.replace ("", "%20");
Picture Download url = new URL (fileName);
URLConnection conn = Url.openconnection ();
OutputStream = Response.getoutputstream ();
InputStream = Conn.getinputstream ();
Ioutils.copy (InputStream, OutputStream);
catch (IOException e) {System.err.println (e);
}finally {ioutils.closequietly (InputStream);
ioutils.closequietly (OutputStream);
}
}
The above is the entire content of this article, I hope to help you learn.