java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽
這篇文章主要介紹了java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
多文件保存到本地:
@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<String, Object> data, HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException { String time = DateUtil.formatFromDate("yyyyMMddHHmmss", new Date()); List<Map<String, Object>> urllist = companyService.findByIMG(data); String path = (String) urllist.get(0).get("imgurl"); String docx = StringUtils.substringAfterLast(path, "."); String fileName = time+"."+docx; // 文件的默認(rèn)保存名 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(); } } /** * 讀取圖片 */ @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(); } } }
前端請(qǐng)求直接拼接圖片路徑即可。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)微信公眾號(hào)消息推送的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用java實(shí)現(xiàn)微信公眾號(hào)消息推送的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(rèn)證的實(shí)現(xiàn)
本文主要介紹了SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(rèn)證的實(shí)現(xiàn),其中也涉及到自定義的過濾器和處理器,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書管理系統(tǒng)
這篇文章主要介紹了Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書館管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04OpenFeign在傳遞參數(shù)為對(duì)象類型是為空的問題
這篇文章主要介紹了OpenFeign在傳遞參數(shù)為對(duì)象類型是為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot整合SpringSecurity認(rèn)證與授權(quán)
在項(xiàng)目開發(fā)中,權(quán)限認(rèn)證是很重要的,尤其是一些管理類的系統(tǒng),對(duì)于權(quán)限要求更為嚴(yán)格,本文主要介紹了SpringBoot整合SpringSecurity認(rèn)證與授權(quán),感興趣的可以了解一下2023-11-11在Java中實(shí)現(xiàn)二叉搜索樹的全過程記錄
二叉樹包含了根節(jié)點(diǎn),孩子節(jié)點(diǎn),葉節(jié)點(diǎn),每一個(gè)二叉樹只有一個(gè)根節(jié)點(diǎn),每一個(gè)結(jié)點(diǎn)最多只有兩個(gè)節(jié)點(diǎn),左子樹的鍵值小于根的鍵值,右子樹的鍵值大于根的鍵值,下面這篇文章主要給大家介紹了關(guān)于如何在Java中實(shí)現(xiàn)二叉搜索樹的相關(guān)資料,需要的朋友可以參考下2022-03-03