首页 > 阿里云的oss使用URL授权上传文件后,文件内容发生变化

阿里云的oss使用URL授权上传文件后,文件内容发生变化

生成临时上传地址的代码如下:

    public static URL getUploadUrl(String path, Long userId, String contentType) {
        Date expires = DateUtils.addHour(new Date(), 1);
        GeneratePresignedUrlRequest request =
                new GeneratePresignedUrlRequest(bucketName, path, HttpMethod.PUT);
        request.setExpiration(expires);
        request.setContentType(contentType);
        request.addUserMetadata("author", userId.toString());
        URL url = ossClient.generatePresignedUrl(request);
        return url;
    }

调用URL,实现文件上传的代码如下:

    private void post(URL url, File file, String ext, Long userId) throws IOException, URISyntaxException {
        HttpClient httpClient= HttpClients.createDefault();
        HttpResponse response;
        HttpPut put=new HttpPut(url.toURI());
        put.addHeader("Content-Type", MimeUtils.getContentType(ext));
        put.addHeader("x-oss-meta-author",userId.toString());

        InputStream is = new FileInputStream(file);
        InputStreamBody body = new InputStreamBody(is, file.getName());
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("file", body)
                .build();

        put.setEntity(reqEntity);
        response=httpClient.execute(put);

        HttpEntity resEntity=response.getEntity();
        if (resEntity!=null){
            String rs = EntityUtils.toString(resEntity, "UTF-8");
            logger.info("--------------------------------------");
            logger.info("Response content: " + rs);
            logger.info("--------------------------------------");
        }
        EntityUtils.consume(resEntity);
    }

我使用该方法上传了一个txt文件,内容为:

1233333
23232233

MimeUtils.getContentType("txt")="text/plain";
在上传成功后通过网页访问,内容变成了:

--Cemr4-Enl8g5d7cSUDJUvYNt7ybMn5FB18n
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

1233333
23232233

--Cemr4-Enl8g5d7cSUDJUvYNt7ybMn5FB18n--

这是什么原因造成的?

【热门文章】
【热门文章】