In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use java to achieve file upload, download, picture preview functions, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Save multiple files locally:
@ ResponseBody @ RequestMapping (value = "/ uploadApp", produces = {"application/json;charset=UTF-8"}, method= RequestMethod.POST) public String uploadApp (HttpServletRequest request,@RequestParam ("file") MultipartFile [] file) throws IOException {try {if (file.length > 0) {String name = file [0] .getOriginalFilename (). Split (";") [0]; String fileUrlName = CommonEnum.FILEPATH+ "/" + name; for (int I = 0; I
< file.length; i++) { FileUtils.copyInputStreamToFile(file[i].getInputStream(), new File(fileUrlName, file[i].getOriginalFilename().split(";")[1])); } return "success"; }else{ return "null"; } }catch (Exception e){ e.printStackTrace(); return "error"; } } 下载文件: @RequestMapping(value = "/download", method = RequestMethod.GET) @ResponseBody public void download(@RequestParam Map data, HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException { String time = DateUtil.formatFromDate("yyyyMMddHHmmss", new Date()); List urllist = companyService.findByIMG(data); String path = (String) urllist.get(0).get("imgurl"); String docx = StringUtils.substringAfterLast(path, "."); String fileName = time+"."+docx; // 文件的默认保存名 InputStream inStream = new FileInputStream(path);// 文件的存放路径 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) >0) response.getOutputStream (). Write (b, 0, len); inStream.close ();} catch (IOException e) {e.printStackTrace ();}} / * * read pictures * / @ RequestMapping (value = "/ iomoreimgcom", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET) @ ResponseBody public synchronized void iomoreimgcom (HttpServletRequest request, HttpServletResponse response) throws Exception {String url = request.getParameter ("url"); File file = new File (url) BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); BufferedOutputStream bos = new BufferedOutputStream (response.getOutputStream ()); response.setHeader ("Content-Type", "image/jpeg"); byte b [] = new byte [1024]; int read; try {while ((read = bis.read (b))! =-1) {bos.write (b, 0, read);} / / request.getRequestDispatcher ("/ components/hazard/yscchird.html") .forward (request, response) } catch (Exception e) {/ / TODO: handle exception} finally {if (bos! = null) {bos.close ();} if (bis! = null) {bis.close ();}
The frontend request is to stitch the image path directly.
The above is "how to use java to achieve file upload, download, picture preview function," all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.