java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽
這篇文章主要介紹了java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽,文中通過示例代碼介紹的非常詳細(xì),對大家的學(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(); } } }
前端請求直接拼接圖片路徑即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:Java
下一篇:Spring實(shí)戰(zhàn)之使用Resource作為屬性操作示例
本文標(biāo)題:java實(shí)現(xiàn)文件上傳、下載、圖片預(yù)覽
本文地址:http://mengdiqiu.com.cn/a1/Java/8756.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時(shí)鐘
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時(shí)鐘
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery