欢迎来到入门教程网!

Java

当前位置:主页 > 软件编程 > Java >

java实现文件上传、下载、图片预览

来源:本站原创|时间:2020-01-10|栏目:Java|点击:

这篇文章主要介绍了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合并list方法代码实例

栏    目:Java

下一篇:Spring实战之使用Resource作为属性操作示例

本文标题:java实现文件上传、下载、图片预览

本文地址:https://www.xiuzhanwang.com/a1/Java/8756.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有