简体   繁体   English

使用http PUT上传的文件在服务器上的大小不正确

[英]Files uploaded using http PUT are not of exact size on the server

I am trying to upload files of different MIME types to AWS S3 using the presigned URL. 我正在尝试使用预先签名的URL将不同MIME类型的文件上传到AWS S3。 I am able to see the document on S3 but the content is inappropriate. 我可以在S3上看到文档,但是内容不合适。 It is either 0 bytes or less than the expected size for every file that I am trying. 它是0字节或小于我尝试的每个文件的预期大小。

Here is the code, please let me know what is missing: 这是代码,请让我知道缺少的内容:

public static void main(String[] args) {

    URLConnection urlconnection = null;

    try {
        File file = new File(File_Path);

        if (isEncoded == "false") {
            URL obj = new URL(Upload_Url);
            urlconnection = obj.openConnection();
        } else {
            try {
                URI uri = new URI(Upload_Url);
                URL url = uri.toURL();
                urlconnection = url.openConnection();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            ((HttpURLConnection) urlconnection).setRequestMethod("PUT");
            ((HttpURLConnection) urlconnection).setRequestProperty("Content-Type", Content_Type);
            ((HttpURLConnection) urlconnection).connect();
        }

        BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) > 0) {
            bos.write(i);
        }
        bis.close();
        bos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

似乎您的流末检测是错误的,因此当您看到空字节时,您将被截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HttpConnection的Http Post获取上传文件的进度 - Http Post using HttpConnection get Progress of the files uploaded 验证上传到服务器的文件 - Validation of files uploaded to the server 如何让服务器下载从客户端的HTTP PUT请求中上传的图像文件? - How to make the server download an image file that was uploaded in an HTTP PUT request from the client? 使用spring下载上载的文件。 Tomcat服务器随机冻结 - Downloading Uploaded Files using spring. Tomcat Server randomly freezes 将用户上传的文件放在播放框架的哪里? - Where to put user uploaded files in play framework? 为什么Google Cloud使用Google应用引擎Java存储的文件大小小于上传的文件大小 - Why Google Cloud Stores files less in size than the uploaded file size using Google app engine Java 在哪里将上传的文件保存在服务器中? - Where to save uploaded files in server? java:使用POST将文件上传到HTTP服务器,服务器代码是否发布? - java : upload files to HTTP server using POST, server code issue? 如何使用JSP将url路径放在服务器中的特定文件中? - How to put the url path to the specific files in server using JSP? 有没有办法列出在服务器上使用JSP上传的文件? - Is there a way to list files uploaded on the server trought JSP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM