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();
        }
    }
}

前端请求直接拼接图片路径即可
原文章地址:https://www.cnblogs.com/-llf/p/12134262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值