Java使用Apache HttpComponents实现上传文件功能

本文探讨了在Spring框架中使用RestTemplate进行POST请求上传文件时,Linux服务器下中文文件名异常的问题。通过引入ApacheHttpComponents库解决,并提供了修复后的代码示例和关键依赖配置。
摘要由CSDN通过智能技术生成

在实际开发过程中,有时候用RestTemplate实现POST请求文件上传后端接口的功能,在本地Windows上自测都是没有问题的;但是部署到测试环境Linux服务器后,上传中文文件名就会出现异常。换用了Apache HttpComponents后就没问题,估计Spring的RestTemplate处理上传功能还有小bug。

pom文件加入相关jar包依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.8</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.8</version>
</dependency>

代码如下:

MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)httpServletRequest;
MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
Charset charset = StandardCharsets.UTF_8;
CharsetEncoder charsetEncoder = charset.newEncoder();
CharBuffer inputBuffer = CharBuffer.allocate(256);
inputBuffer.put(multipartFile.getOriginalFilename()).flip();
ByteBuffer outputBuffer = ByteBuffer.allocate(1024);
charsetEncoder.encode(inputBuffer, outputBuffer, true);
outputBuffer.flip();
String fileName = charset.decode(outputBuffer).toString();
File file = new File(fileName);
log.info("request upload url={} fileName={}", url, fileName);
try (FileOuputStream fos = new FileOuputStream(file)) {
    fos.write(multipartFile.getBytes());
    fos.flush();
} catch (IOException ex) {
    log.error("request upload url={} fileName={} exception:", url, fileName, ex);
    throw ex;
}
String result = "";
String boundary = "||";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setCharset(charset);
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.setBoundary(boundary);
    builder.addPart("file", new FileBody(file));
    HttpEntity entity = builder.build();
    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity responseEntity = response.getEntity();
    if (response.getStatusLine().getStatusCode() == 200) {
        log.info("upload url={} fileName={} succeed", url, fileName);
        result = EntityUtils.toString(responseEntity, charset);
        return result;
    } else {
        log.error("upload url={} fileName={} failed, status={}", url, fileName, response.getStatusLine().getStatusCode());
        return null;
    }
} catch(Exception ex) {
    log.error("upload url={} fileName={} exception:", url, fileName, ex);
    throw ex;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值