温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用java实现文件上传、下载、图片预览功能

发布时间:2021-09-26 18:01:18 来源:亿速云 阅读:124 作者:小新 栏目:编程语言

小编给大家分享一下如何使用java实现文件上传、下载、图片预览功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

多文件保存到本地:

@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; // 文件的默认保存名    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();      }    }  }

前端请求直接拼接图片路径即可。

以上是“如何使用java实现文件上传、下载、图片预览功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI