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

时间:2017-10-13 11:06:28

标签: java http file-upload put

我正在尝试使用预签名网址将不同MIME类型的文件上传到AWS S3。我能够在S3上看到该文档,但内容不合适。它是0字节或小于我正在尝试的每个文件的预期大小。

以下是代码,请告诉我遗漏的内容:

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();
    }

1 个答案:

答案 0 :(得分:1)

您的流末尾检测似乎是错误的,因此您在看到空字节时会截断。