Java.SpringBoot InputStream流复制.

本文档产生场景.

在项目中,由于需要一个服务做为文件中转,先定义这个中转服务为C。C服务需要将内部文件服务器(这里定义为A)的文件转移到新的文件服务器(定义为B中)。由于编程经验不足,在C中直接将A的文件读成inputStream流,然后将此inputStream流交给B。在测试环境,由于网络正常,没有出现过任何问题,或者说问题不明显,但在生产环境中,由于网络速度的不对称,造成上传文件时好时坏。以下,将些问题进行记录。

这个问题出现,会报以下错误:

Premature end of Content-Length delimited message body (expected: 11,791,936; received: 3,011,273)
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
        at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297)
        at org.apache.catalina.connector.Request.parseParts(Request.java:2884)
        ... 33 more
1. 问题代码

由于不能将所有代码都复制到这里,因此,这里的代码可以认为是半伪代码.

CloseableHttpClient httpClient = HttpClients.createDefault();
// 设置CloseableHttpClient的超时时间.
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(100000)
                    .setSocketTimeout(100000)
                    .setStaleConnectionCheckEnabled(true)
                    .setConnectTimeout(100000).build();
httpPost.setConfig(requestConfig);
// uploadUrl  这个是上传服务器,也是在前面问题说到的B服务地址.
HttpPost httpPost = new HttpPost(uploadUrl);
try {
   // 此部分只是一个下载地址.
   // 可以认为是执行下载.
    InputStream inputStreamTemp = 从A服务器获取的InputStream流
    // 将获取到的inputStreamTemp流放入BufferedInputStream.
    // 这一步如果只针对下载文件,可以很好的提高效率,但是如果做为一个中转,是没有办法实现的.
    BufferedInputStream inputStreamBuffer = new BufferedInputStream(inputStreamTemp,1024*1024*100);
    // 将inputStreamBuffer放入HttpEntity, 这里的代码未写全,如果需要,可根据实际情况进行写.
    HttpEntity reqEntity = MultipartEntityBuilder.create().addBinaryBody(
                    "file", inputStreamBuffer,
                    ContentType.MULTIPART_FORM_DATA
                  ).build();
    httpPost.setEntity(reqEntity);
    //执行上传
    CloseableHttpResponse response = httpClient.execute(httpPost);
}

上面的代码,如果在测试环境,不会出任何问题,因在测试环境的网络非常好,下载和上载都有很高的速度,但是放在生产环境,A和B不在一个网段,因此,这样操作就会时不时的出现问题。

以上就是这段代码的问题所在。接下来,我们需要对这段代码进行更改,让其可以在生产环境中使用。

当然,有人会问到,为什么不先把文件下载到本地,然后再把文件上传到服务器呢?
答: 不想在中转服务器生成任何文件或临时文件,不喜欢。

2. 正常代码

CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(100000)
                    .setSocketTimeout(100000)
                    .setStaleConnectionCheckEnabled(true)
                    .setConnectTimeout(100000).build();
httpPost.setConfig(requestConfig);
HttpPost httpPost = new HttpPost(uploadUrl);
try {
    InputStream inputStreamTemp = 从A服务器获取的InputStream流
    
    **以上都没有作改变,所以把注释就删除了**

   // 创建一个ByteArrayOutputStream,用于存放复制流.
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    // 将从A服务器获取的InputStream流inputStreamTemp 复制到 byteArrayOutputStream流中.
    byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStreamTemp.read(buffer)) > -1) {
                byteArrayOutputStream.write(buffer,0,len);
            }
            byteArrayOutputStream.flush();

    // 接下来,将复制的流byteArrayOutputStream转换成上传流uploadInputStream
    InputStream uploadInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    // 将inputStreamBuffer放入HttpEntity, 这里的代码未写全,如果需要,可根据实际情况进行写.
    HttpEntity reqEntity = MultipartEntityBuilder.create().addBinaryBody(
                    "file", inputStreamBuffer,
                    ContentType.MULTIPART_FORM_DATA
                  ).build();
    httpPost.setEntity(reqEntity);
    //执行上传
    CloseableHttpResponse response = httpClient.execute(httpPost);
}

以上就是流复制的简单方法,以及代码.

欢迎关注,及提问.


如果有什么问题,可以加入QQ群进行讨论。QQ群:839421316

谢谢支持。
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java中的URLConnection和InputStream来下载网络图片。以下是一个使用Spring Boot的示例代码: ```java import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; @RestController public class ImageController { @GetMapping("/download/{url}") public String downloadImage(@PathVariable String url) { try { // 创建URL对象 URL imageUrl = new URL(url); // 打开连接 URLConnection connection = imageUrl.openConnection(); // 设置请求属性,防止被网站拒绝访问 connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // 获取输入 InputStream inputStream = connection.getInputStream(); // 使用Spring的StreamUtils复制输入到输出 String localPath = "path/to/save/image.jpg"; FileOutputStream outputStream = new FileOutputStream(localPath); StreamUtils.copy(inputStream, outputStream); // 关闭 outputStream.close(); inputStream.close(); return "Image downloaded successfully!"; } catch (IOException e) { e.printStackTrace(); return "Failed to download image."; } } } ``` 在上述示例中,我们创建了一个`ImageController`来处理下载图片的请求。在`downloadImage`方法中,我们首先创建一个URL对象,然后打开连接并设置请求属性以避免被网站拒绝访问。接着,我们获取输入并使用Spring的`StreamUtils`将其复制到输出中,最后关闭。 你可以将上述代码放到一个Spring Boot项目中,并通过访问`/download/{url}`来下载指定URL的图片。记得将`path/to/save/image.jpg`替换为你想要保存图片的本地路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值