zoukankan      html  css  js  c++  java
  • Fileupload文件上传下载

    package com.lhb.web.controller;

    import com.lhb.service.file.FileUploadService;
    import com.lhb.support.ApiResponse;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.http.HttpStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import org.springframework.web.multipart.MultipartFile;


    import javax.servlet.http.HttpServletResponse;
    import java.util.Map;


    @RestController
    @RequestMapping("/api/file")
    @Slf4j
    public class FileController {


        @Autowired
        private FileUploadService fileUploadService;


        @PostMapping("/fileUpload")
        @ApiOperation(value = "文件上传")
        public ApiResponse fileUpload(MultipartFile file) throws Exception {
            Map<String,String> fileMap = fileUploadService.fileUpload(file);
            return ApiResponse.ofSuccess(fileMap);
        }

       @GetMapping("/fileDownload")
        @ApiOperation(value = "文件下载")
      public ApiResponse fileDownload(@RequestParam(value = "filePath") String filePath, @RequestParam(value =         "fileName") String fileName,   HttpServletResponse response) throws Exception {
            fileUploadService.fileDownload(filePath, fileName, response);
            return ApiResponse.ofMessage(HttpStatus.SC_OK, "文件下载成功");
        }

    }


    package com.lhb.service.file;

    import org.springframework.web.multipart.MultipartFile;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Map;

    public interface FileUploadService {
        Map<String, String> fileUpload(MultipartFile file) throws IOException;
        void fileDownload(String filePath, String fileName, HttpServletResponse response)throws IOException;

    }



    package com.lhb.service.file;

    import com.lhb.utils.PathUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;

    @Service
    @Slf4j
    public class FileUploadServiceImpl implements FileUploadService {
        @Override
        public Map<String, String> fileUpload(MultipartFile file) throws IOException {
            Map<String, String> fileMap = new HashMap<>(2);
            if (!file.isEmpty()) {
                String fileName = file.getOriginalFilename();
                String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                // 文件上传路径
                String filePath = PathUtil.getClasspath() + "uploadFiles/" + trueFileName;
                // 转存文件到指定的路径
                file.transferTo(new File(filePath));
                fileMap.put("fileName", fileName.substring(0, fileName.lastIndexOf(".")));
                fileMap.put("filePath", trueFileName);
                log.info("【fileupload】处理结果-----------, fileMap={}", fileMap);
            }
            return fileMap;
        }

        @Override
        public void fileDownload(String filePath, String fileName, HttpServletResponse response) throws IOException {
            String realFilePath = PathUtil.getClasspath() + "uploadFiles/" + filePath;
            File file = new File(realFilePath);
            if (file.exists()) {
                int fileLength = (int) file.length();
                if (fileLength != 0) {
                    String suffix = filePath.substring(filePath.lastIndexOf("."));
                    String realFileName = fileName + suffix;
                    realFileName = new String(realFileName .getBytes(), "ISO-8859-1");
                    response.reset();
                    response.setContentType("application/octet-stream");

                    response.setHeader("Content-disposition", "attachment;filename=" + realFileName);
                    response.setContentLength(fileLength);
                    InputStream is = new FileInputStream(realFilePath);
                    byte[] buffer = new byte[1024];
                    ServletOutputStream fos = response.getOutputStream();
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }
                    is.close();
                    fos.flush();
                    fos.close();
                }
            }
        }
    }

  • 相关阅读:
    django学习笔记
    linux安装tree命令
    SecureCRT卡死的问题
    vim常用快捷键记录
    项目经理应具备的9种能力
    regsvr32命令
    通知栏构建和取消的基本认识
    AlertDialog 对话框 5种
    异步任务——AsyncTask的初步认识
    BaseAdapter的使用与优化
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13300643.html
Copyright © 2011-2022 走看看