This article shares with you the method of downloading pictures in Java server for your reference. The specific content is 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 the image from the server* * @param fileName Image address* @param response * @return */ @RequestMapping(value = "/download") 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"); //handle browser compatibility response.setContentType("application/msexcel;charset=utf-8");//Define the output type Enumeration enumeration = request.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")); } // If there is a space in the url address, an error will be reported! The solution is: use + or %20 instead of spaces in the url parameter. fileName = fileName.replace(" ", "%20"); //Image download URL 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 all about this article, I hope it will be helpful to everyone's learning.