文件上传不适用于angular-file-upload和Spring [英] File upload won't work with angular-file-upload and Spring
问题描述
我正在尝试进行简单的文件上传,但Spring不想和我一起玩。
这是文件上传的端点 - 目前还没有很多:
@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method = RequestMethod.POST,consumemes = { multipart / form-data})
public ResponseEntity< WordVectorListDto> uploadModel(
@ModelAttribute(file)MultipartFile文件,
// @RequestBody对象obj,
RedirectAttributes redirectAttributes){
LOGGER.debug(POST uploadModel );
返回新的ResponseEntity< WordVectorListDto>((WordVectorListDto)null,HttpStatus.OK);
}
我已经尝试了几件事,但最终都出现了不同的错误。我刚刚尝试使用 @RequestBody
,因为我觉得这可能就是伎俩,但后来我得到一个例外说法:
< pre class =lang-none prettyprint-override>
内容类型'multipart / form-data; boundary = ---- WebKitFormBoundarycV8dFSvDV6U9OwJq'不支持
或
内容类型'multipart / form-data'不支持
取决于我刚尝试的内容。
如果我使用 @RequestPart(file)MultipartFile文件
我看到
所需的请求部分'文件'不存在
类似于 @RequestParam(file)
。
我不知道这有多难我希望有人可以告诉我如何从客户端获取该文件。
下面你可以看到我发送给端点的请求:
我这个请求好吗?
Web客户端:
var uploader = $ scope.uploader = new FileUploader({
url:'http:// localhost:8080 / rest-api / dl4j / we / uploadModel'
});
uploader.onAfterAddingFile = function($ modelFile){
console.info('onAfterAddingFile',$ modelFile);
var fd = new FormData();
fd.append('file',$ modelFile.file);
$ http.post($ modelFile.url,fd,{
header:{
'Content-Type':undefined
},
transformRequest :angular.identity
})
.then(
函数(数据){
alert(上传成功);
},
函数(数据,状态){
alert(上传错误);
}
);
};
index.html
< label class =btn btn-default btn-filestyle =float:right;>
上传
< input
type =file
style =display:none;
name =file
multiple
nv-file-select
uploader =uploader>
< / label>
以下是我的意思:
@RequestMapping(value =/ uploadFile,method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam(file)MultipartFile文件){
String name =test11;
if(!file.isEmpty()){
try {
byte [] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + - uploaded)));
stream.write(bytes);
stream.close();
返回您已成功上传+名称+进入+名称+ - 上传!;
} catch(例外e){
返回你上传失败+ name +=>+ e.getMessage();
}
}其他{
返回您无法上传+名称+因为文件为空。;
}
}
并且不要忘记注册多部分解析器:
@Bean
public MultipartResolver multipartResolver(){
org.springframework.web .multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10000000);
返回multipartResolver;
}
这里是html代码...看一看输入字段的名称/ ID ..
要上传的文件:
Name1:< input type =textname =name>
要上传的文件2:< input type =filename =file>
Name2:< input type =textname =name>
< input type =submitvalue =Upload>按此处上传文件!
< / form>
I am trying to make a simple file upload possible but Spring does not want to play with me.
This is the endpoint for file uploads - currently not doing a lot:
@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method=RequestMethod.POST, consumes = {"multipart/form-data"})
public ResponseEntity<WordVectorListDto> uploadModel(
@ModelAttribute("file") MultipartFile file,
// @RequestBody Object obj,
RedirectAttributes redirectAttributes) {
LOGGER.debug("POST uploadModel");
return new ResponseEntity<WordVectorListDto>((WordVectorListDto)null, HttpStatus.OK);
}
I've tried several things but it all ends up in different errors. I've just tried to use @RequestBody
because I thought maybe that's the trick but then I get an exception saying:
Content type 'multipart/form-data;boundary=----WebKitFormBoundarycV8dFSvDV6U9OwJq' not supported
or
Content type 'multipart/form-data' not supported
depending on what I just tried.
If I go with @RequestPart("file") MultipartFile file
I see
Required request part 'file' is not present
which is similar for @RequestParam("file")
.
I have no idea what's so hard on this but I hope somebody can tell me how I can get that file from the client.
Below you can see the request I've sent to the endpoint:
Is this request okay?
Web Client:
var uploader = $scope.uploader = new FileUploader({
url: 'http://localhost:8080/rest-api/dl4j/we/uploadModel'
});
uploader.onAfterAddingFile = function($modelFile) {
console.info('onAfterAddingFile', $modelFile);
var fd = new FormData();
fd.append('file', $modelFile.file);
$http.post($modelFile.url, fd, {
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity
})
.then(
function (data) {
alert("upload success");
},
function (data, status) {
alert("upload error");
}
);
};
index.html
<label class="btn btn-default btn-file" style="float:right;">
Upload
<input
type="file"
style="display: none;"
name="file"
multiple
nv-file-select
uploader="uploader">
</label>
Here is how i didi it:
@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
and dont forget to register the multipart resolver:
@Bean
public MultipartResolver multipartResolver() {
org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10000000);
return multipartResolver;
}
here is the html code ... take a look at the name/id of the input fields .. File1 to upload:
Name1: <input type="text" name="name">
File2 to upload: <input type="file" name="file">
Name2: <input type="text" name="name">
<input type="submit" value="Upload"> Press here to upload the file!
</form>
这篇关于文件上传不适用于angular-file-upload和Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!