java上传文件及下载文件

Controller


@Api(tags = "附件上传")
//@UserLoginToken
@RestController
@RequestMapping("/fileController")
public class FileController {
    @Autowired
    private Config config;
    @ApiOperation("上传")
    @RequestMapping("/uploadFile")
    @ResponseBody
    public Response<String> uploadFile(@RequestParam MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //系统目录
        String filePath = System.getProperty("user.dir") + config.getPicPath();
        java.io.File dest = new java.io.File(filePath + fileName);
        java.io.File pfile = new java.io.File(filePath);
        if (!pfile.exists()) {
            pfile.mkdirs();
        }
        if(dest.exists()){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddmmss");
            fileName=sdf.format(Calendar.getInstance().getTime())+fileName;
            dest=new java.io.File(filePath+fileName);
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response<String>(ExpCodeState.upload_success, fileName);
    }

    /**
     * 下载
     * @param filename
     * @param response
     */
    @ApiOperation("下载")
    @RequestMapping("/{filename:.+}")
    @ResponseBody
    public void outPic(@PathVariable String filename, HttpServletResponse response) {
        java.io.File dest = new java.io.File(System.getProperty("user.dir") + config.getPicPath() + filename);
        try {
            ServletOutputStream out;
            try {
                @SuppressWarnings("resource")
                FileInputStream fis = new FileInputStream(dest);
                out = response.getOutputStream();
                int bytesRead = 0;
                byte[] buffer = new byte[819200];
                while ((bytesRead = fis.read(buffer, 0, 819200)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

config.properties 配置文件

property.picPath=/../negativeChecklist/

访问路径

http://xxx.xxx.x.xxx:8080/pmInfoApi/fileController/接口(1).txt

实体类

/**
 * Title: config.java
 * @author WuJin
 * @date 2020年3月28日
 * @version 1.0
 */
package com.pm.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import lombok.Data;

@PropertySource(value = { "classpath:config.properties" })
@ConfigurationProperties(prefix = "property")
@Component
@Data
public class Config {

	private String picPath;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值