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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丿乐灬学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值