项目总结第八天

Feign文件上传

导入jar

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>2.1.0</version>
</dependency>

client

@FeignClient(value = "HRM-COMMON",configuration=FeignMultipartSupportConfig.class,fallbackFactory = FastDfsClientFallbackFactory.class)
@RequestMapping("/fastDfs")
public interface FastDfsClient {
    //上传
    @PostMapping(produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    AjaxResult upload(@RequestPart(required =true,value = "file")MultipartFile file);

    //下载 以Reponse来接收数据
    @GetMapping
    Response download(@RequestParam(required = true,value = "path")String path);

}

fallbackFactory

@Component
public class FastDfsClientFallbackFactory implements FallbackFactory<FastDfsClient> {
    @Override
    public FastDfsClient create(Throwable throwable) {
        return new FastDfsClient() {
            @Override
            public AjaxResult upload(MultipartFile file) {
                return new AjaxResult().setSuccess(false).setMessage("上传失败");
            }

            @Override
            public Response download(String path) {
                return null;
            }
        };
    }
}

config

@Configuration
public class FeignMultipartSupportConfig {
    //form编码格式
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder(){
        return  new SpringFormEncoder();
    }
    @Bean
    public feign.Logger.Level multipartLoggerLevel(){
        return Logger.Level.FULL;
    }
}

service

@PostMapping(value = "/upload", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
        , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload(@RequestPart(value = "file") MultipartFile file) {

调用

!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

@Autowired
private FastDfsClient fastDfsClient;
@Test
public void test1() throws Exception{
    FileItem fileItem = createFileItem(new File("C:\\Users\\Administrator\\Desktop\\问题.txt"));
    MultipartFile mfile = new CommonsMultipartFile(fileItem);
    fastDfsClient.upload(mfile);
}
/*
创建FileItem
 */
private FileItem createFileItem(File file) {
    FileItemFactory factory = new DiskFileItemFactory(16, null);
    String textFieldName = "textField";
    FileItem item = factory.createItem("file", "text/plain", true, file.getName());
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    try {
        FileInputStream fis = new FileInputStream(file);
        OutputStream os = item.getOutputStream();
        while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return item;
}

Feigin下载

Feign

//获取用户
@RequestMapping(value = "/download",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response download(@RequestParam("path")String path); //直接把流写到response

Service

@GetMapping(value = "/download",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void download(@RequestParam("path")String path, HttpServletResponse response) {
    String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
    String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
    String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// xxxxx/yyyy
    System.out.println(groupName);
    System.out.println(remotePath);
    OutputStream os = null;
    InputStream is = null;
    try {
        byte[] datas = FastDfsApiOpr.download(groupName, remotePath);
        os = response.getOutputStream(); //直接给以流方式进行返回
        is = new ByteInputStream(datas,datas.length);
        IOUtils.copy(is,os);

调用

Response response =
        fastDfsClient.download(templateUrl); //通过fastdfs下载压缩包
String tmpdir=System.getProperty("java.io.tmpdir");
System.out.println(tmpdir);
String zipName = tmpdir+"/temp.zip";

os = new FileOutputStream(zipName);
is = response.body().asInputStream();
IOUtils.copy(is , os); //保存到本地
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值