Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload a picture to the webapp path by Java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how Java can upload pictures to the webapp path. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Upload the image to the webapp path to get the path.

This method is obtained under the project webapp folder.

String contexPath= request.getSession (). GetServletContext () getRealPath ("/")

Get the IP address, port number and project name

Spring java gets the file path @ RequestMapping ("/ act/worldcup_schedule_time/imgdownload") @ ResponseBody public String scheduleDownload (HttpServletRequest request, HttpServletResponse response, HttpSession session) {response.setCharacterEncoding ("UTF-8"); String downLoadName = "worldcup.jpg"; InputStream input = null; try {request.setCharacterEncoding ("UTF-8") under webapp / / get the path to the file / / String url = session.getServletContext () .getRealPath ("/") + "resources\\ images\\ act\\ worldcup_merge\\ worldcup720.png"; String url = session.getServletContext () .getRealPath ("/") + "resources/images/act/worldcup_merge/worldcup720.png"; System.out.println (url); File file = new File (url) Input = FileUtils.openInputStream (file); byte [] data = IOUtils.toByteArray (input); / / System.out.println ("file name:" + downLoadName); response.reset (); / / set the response header information (Chinese problem resolution) response.setHeader ("content-disposition", "attachment") FileName= "+ URLEncoder.encode (downLoadName," UTF-8 "); response.addHeader (" Content-Length ","+ data.length); response.setContentType (" image/png; charset=UTF-8 "); IOUtils.write (data, response.getOutputStream ());} catch (Exception e) {logger.error (" error downloading picture ") If (input! = null) {IOUtils.closeQuietly (input);}} return null;} is here on how to upload images to the webapp path by Java. I hope the above content can help you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report