Java / Spring - 来自 InputStream 的零文件长度

Java / Spring - zero file length from InputStream

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

这是 client-side 代码。

$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)
}

这是 server-side 代码,带有所有 @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;
}

最后,这是我得到的印刷品 server-side:

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

阅读更多 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;
}