服务端文件接收与上传

服务端接收大文件步骤(Spring框架)

  1. pom文件引入依赖
		<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
  1. springMVC配置文件增加如下配置
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
  1. Controller层接收参数类型
	@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result upload(@RequestParam("file") MultipartFile file) {
        return Result.create(service.file(file));
    }

服务端将文件上传到远程服务器

  1. 远程服务接收文件为file格式
/**
     * 上传文件到远程服务器
     * @param url  			 远程服务接口地址
     * @param file 			 源文件
     * @param fileParamName  远程服务接收文件参数名
     * @return string		 网络可访问的文件地址
     */
public staic String uploadFile(String url, MultipartFile file, String fileParamName) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        try {
            String fileName = file.getOriginalFilename();
            HttpPost httpPost = new HttpPost(url);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(StandardCharsets.UTF_8);
            //加上此行代码解决返回中文乱码问题
            builder.setMode(HttpMultipartMode.RFC6532);
            // 文件流
            builder.addBinaryBody(fileParamName, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            // 执行提交
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

2.远程服务接收文件为流格式

/**
     * 上传inputStream到远程服务器
     * @param url           远程服务接口地址
     * @param stream        流文件
     * @param fileName      文件名称
     * @return
     */
    public static String uploadInputStream(String url, InputStream stream, String fileName) {

        CloseableHttpResponse execute = null;
        String result = "";
        try {
            HttpPost post = new HttpPost(url + fileName);
            post.setEntity(new ByteArrayEntity(IOUtils.toByteArray(stream)));
            CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
            execute = closeableHttpClient.execute(post);
            result = EntityUtils.toString(execute.getEntity());
            JSONObject object = JSONObject.parseObject(result);
            result = object.getString("result");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

附:业务中经常使用文件处理上传,建议将方法写在一个工具类中,且用static修饰,则业务中有需要用到的地方可以直接通过类调用此方法(类.方法名)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值