zero file length from InputStream
I have a REST application with a client built in Angular and a server built with Spring. I'm trying to send a file with an async request, but I receive a 0-length file. It's strange because in the header I can read the exact size of the file (but not its name) but I have no byte stream.
This is the client-side code.
$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)
}
This is the server-side code, with all the @RequestMapping signature:
@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;
}
And, finally, this is the print I get server-side:
null //content-range
15021 //content-length - perfect!
null //content-disposition
multipart/form-data; boundary=----WebKitFormBoundary3F1AuFnuZQqJTRK8 //content-type
Ret: 0 Bytes // :(
Where am I wrong with this code?
Thanks in advance!
 Add @RequestParam("uploadedFile") MultipartFile file to your controller params.  Then you can access the bytes  
byte[] bytes = file.getBytes();
Read more here or here
仅供参考,以下是适用于我的代码,通过多个文件处理得到改进:
@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;
}
上一篇: 如何在块中使用Spring REST multipart发送大文件(REST客户端)
下一篇: 来自InputStream的零文件长度
