来自InputStream的零文件长度

我有一个REST应用程序,内含Angular构建的客户端和使用Spring构建的服务器。 我试图发送一个异步请求的文件,但我收到一个长度为0的文件。 这很奇怪,因为在头文件中我可以读取文件的确切大小(但不是它的名字),但是我没有字节流。

这是客户端代码。

$scope.uploadFile = function() {
    var fd = new FormData()
    for (var i in $scope.files) {
        fd.append("uploadedFile", $scope.files[i])
    }
    var xhr = new XMLHttpRequest()
    xhr.upload.addEventListener("progress", uploadProgress, false)
    xhr.addEventListener("load", uploadComplete, false)
    xhr.addEventListener("error", uploadFailed, false)
    xhr.addEventListener("abort", uploadCanceled, false)
    xhr.open("POST", CONST_SERVER_HOST+"/upload/?token="+$scope.token)
    $scope.progressVisible = true
    xhr.send(fd)
}

这是服务器端代码,具有所有@RequestMapping签名:

@RequestMapping(value = "/upload/", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
public String uploadChunked(
        final HttpServletRequest request,
        final HttpServletResponse response) {
    String res;
    System.out.println(request.getHeader("content-range"));
    System.out.println(request.getHeader("content-length"));
    System.out.println(request.getHeader("content-disposition"));
    System.out.println(request.getHeader("content-type"));
    InputStream lettura = null;
    OutputStream scrittura;
    try
    {
        File targetFile = new File("file.pdf");
        OutputStream outStream = new FileOutputStream(targetFile);
        lettura = request.getInputStream();
        byte[] buffer = new byte[1024];
        int len;
        int total = 0;
        while ((len = lettura.read(buffer)) != -1) {
            total += 1024;
            System.out.println("content" + len);
            outStream.write(buffer, 0, len);
        }
        lettura.close();
        outStream.close();
        res = total+" Bytes";
    }
    catch (IOException e)
    {
        e.printStackTrace();
        res = "exception";
    }
    System.out.println("Ret: "+res);
    return res;
}

最后,这是我得到服务器端的打印:

null //content-range
15021 //content-length - perfect!
null //content-disposition
multipart/form-data; boundary=----WebKitFormBoundary3F1AuFnuZQqJTRK8 //content-type
Ret: 0 Bytes // :(

这个代码我错在哪里?

提前致谢!


@RequestParam("uploadedFile") MultipartFile file到您的控制器参数中。 然后你可以访问字节

byte[] bytes = file.getBytes();

阅读更多在这里或这里


仅供参考,以下是适用于我的代码,通过多个文件处理得到改进:

@RequestMapping(value = "/upload/", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
public String uploadChunked(final HttpServletRequest request)
{

    String res;
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    try
    {
        InputStream lettura;
        Iterator<String> iterator = multipartRequest.getFileNames();
        int totalBytes = 0;
        while (iterator.hasNext())
        {
            System.out.println("");
            String key = iterator.next();

            String filename = multipartRequest.getFile(key).getOriginalFilename();
            System.out.println("Name: "+filename);
            System.out.println("Size: "+multipartRequest.getFile(key).getSize());
            System.out.println("File name: "+multipartRequest.getFile(key).getName());
            lettura = multipartRequest.getFile(key).getInputStream();

            File targetFile = new File("uploads/"+filename);
            OutputStream outStream = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = lettura.read(buffer)) != -1) {
                totalBytes += len;
                outStream.write(buffer, 0, len);
            }
            lettura.close();
            outStream.close();
        }
        res = "Received "+totalBytes+" bytes";
    }
    catch (IOException e)
    {
        e.printStackTrace();
        res = "exception";
    }
    return res;
}
链接地址: http://www.djcxy.com/p/48739.html

上一篇: zero file length from InputStream

下一篇: how to use multipartfile in spring controller?