zoukankan      html  css  js  c++  java
  • 微服务中的文件上传下载

    https://blog.csdn.net/weixin_34235457/article/details/88231392

    文件上传下载rest接口定义
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.util.StrUtil;
    import cn.hutool.crypto.digest.DigestUtil;

    @Api(value = "文件controller", tags = {"文件上传下载接口"})
    @RestController
    @RequestMapping("/file")
    public class FileController{

    private static final String SAVE_PATH = System.getProperty("user.home");
    /**
    * 上传文件
    *
    * @param file
    * @return
    */
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public String upload(@NotNull MultipartFile file) {
    //文件MD5值
    String md5File = DigestUtil.md5Hex(file.getInputStream());
    //文件后缀
    String extName = StrUtil.subAfter(file.getOriginalFilename(), ".", true);
    //文件如果不存在,则保存,否则直接返回文件的MD5名
    File localFile = new File(SAVE_PATH, md5File)
    if (!FileUtil.exist(localFile)) {
    //创建一个新文件
    File attachFile = FileUtil.touch(SAVE_PATH, md5File);
    //将文件流写入文件中
    FileUtil.writeFromStream(file.getInputStream(), attachFile);
    return "文件上传成功"
    }
    return "文件已存在";
    }



    /**
    * 文献下载
    *
    * @return
    */
    @ApiOperation(value = "求助文件下载")
    @ApiImplicitParam(name = "fileName", value = "文件名", dataType = "String", paramType = "query")
    @GetMapping("/download")
    public ResponseEntity download(@RequestParam String fileName) {
    File file = new File(SAVE_PATH, fileName);
    if (!file.exists()) {
    return ResponseEntity.notFound().build();
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    //chrome浏览器下载文件可能出现:ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,
    //产生原因:可能是因为文件名中带有英文半角逗号,
    //解决办法:确保 filename 参数使用双引号包裹[1]
    headers.add("Content-Disposition", "attachment; filename="" + file.getName()+""");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");
    return ResponseEntity
    .ok()
    .headers(headers)
    .contentLength(downloadModel.getDocFile().length())
    .contentType(MediaType.parseMediaType("application/octet-stream"))
    .body(new FileSystemResource(file));
    }
    }
    restTemplat调用api上传
    public void uploadFile(@NotNull MultipartFile file){
    File newFile = FileUtil.writeFromStream(file.getInputStream(),"F:/upload/"+file.getOriginalFilename());
    FileSystemResource fs =new FileSystemResource(newFile);
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    //这里不能直接传参File或MultipartFile,需要用FileSystemResource包装一下
    param.add("file",fs);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(param,headers);
    ResponseEntity<String> responseEntity = new RestTemplate().exchange("http://cloud.test.hnlat.com/zuul/resources-server/file/upload", HttpMethod.POST, httpEntity,String.class);
    String fileName = responseEntity.getBody().toString();
    }
    附:
    [1]http://codeverge.com/embarcadero.delphi.intraweb/chrome-err_response_headers_multi/1061174
    ---------------------
    作者:weixin_34235457
    来源:CSDN
    原文:https://blog.csdn.net/weixin_34235457/article/details/88231392
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    用Repeater控件显示主从关系数据表
    transactsql高级查询(上)
    利用动态加载模板,配合ajax实现无刷新操作
    如何使用Repeater控件的模板
    总结一下DataGrid,DataList,Repeater
    ajax实现dropdownlist与datagrid或Repeater无联动刷新
    Asp.net2.0 VS 2005下的repeater控件本功能分页实例(共有 条记录 共有几页 当前第 页 首页,上一页,下一页,尾页 DropDownList跳转)
    SQLSERVER存储过程
    我的java 的实用代码
    各种数据库对应的jar包、驱动类名和URL格式
  • 原文地址:https://www.cnblogs.com/linus-tan/p/11132487.html
Copyright © 2011-2022 走看看