我有一个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 // :(
这个代码在哪里错了?
提前致谢!
答案 0 :(得分:0)
将@RequestParam("uploadedFile") MultipartFile file
添加到控制器参数中。然后你可以访问字节
byte[] bytes = file.getBytes();
答案 1 :(得分:0)
仅供参考,以下是适用于我的代码,通过多个文件处理进行了改进:
@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;
}