在上传多个MultipartFile文件的Spring Boot中遇到错误。

huangapple go评论108阅读模式
英文:

getting error on springboot while upload list of multipartfile

问题

  1. 我正在尝试上传多个文件,在这样做时遇到了问题。请有人指出可能出了什么问题吗?
  2. 我在此附上了我代码中相关的片段,以便更好地进行调试。
  3. **HTML代码**
  4. <label>
  5. 欢迎 {{name}},欢迎来到新的应用。
  6. </label>
  7. <div>
  8. <input type="file" multiple placeholder="选择要上传的文件" accept=".xlsx" (change)=selectedfiles($event)>
  9. </div>
  10. **上传逻辑**
  11. selectedfiles(event){
  12. this.selectedxlfiles=event.target.files;
  13. this.fileandinstancekeyobj.filetoupload=this.selectedxlfiles;
  14. this.fileandinstancekeyobj.instancekey=this.instancekey;
  15. this.uploadservice.uploadtoserver(this.fileandinstancekeyobj).subscribe(result=>{
  16. console.log(result);
  17. })
  18. }
  19. **上传服务**
  20. uploadtoserver(selectedfileandinstacekeyobj): Observable<HttpEvent<{}>>{
  21. let url:string=environment.url+'uploadfile';
  22. const newrequest=new HttpRequest('POST',url,selectedfileandinstacekeyobj,{
  23. reportProgress:true,
  24. responseType:'text'
  25. });
  26. return this.http.request(newrequest);
  27. }
  28. **Spring Boot控制器**
  29. @RestController
  30. public class uploadcontroller {
  31. @PostMapping("/uploadfile")
  32. public ResponseEntity<String> handleupload(@RequestBody uploaddto dto){
  33. System.out.println("成功");
  34. System.out.println(dto.getInstancekey()+" "+dto.getFiletoupload().length);
  35. return ResponseEntity.status(HttpStatus.OK).body("ok");
  36. }
  37. **上传DTO**
  38. import java.util.ArrayList;
  39. import java.util.LinkedList;
  40. import java.util.List;
  41. import org.springframework.web.multipart.MultipartFile;
  42. class uploaddto {
  43. List<MultipartFile> filetoupload;
  44. String instancekey;
  45. public uploaddto(List<MultipartFile> filetoupload, String instancekey) {
  46. super();
  47. filetoupload=new ArrayList<MultipartFile>();
  48. this.filetoupload = filetoupload;
  49. this.instancekey = instancekey;
  50. }
  51. public List<MultipartFile> getFiletoupload() {
  52. return filetoupload;
  53. }
  54. public void setFiletoupload(List<MultipartFile> filetoupload) {
  55. this.filetoupload = filetoupload;
  56. }
  57. public String getInstancekey() {
  58. return instancekey;
  59. }
  60. public void setInstancekey(String instancekey) {
  61. this.instancekey = instancekey;
  62. }
  63. }
  64. 我收到以下错误消息
  65. [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
  66. Cannot deserialize instance of `java.util.ArrayList<org.springframework.web.multipart.MultipartFile>` out of START_OBJECT token;
  67. nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
  68. Cannot deserialize instance of `java.util.ArrayList<org.springframework.web.multipart.MultipartFile>` out of START_OBJECT token
  69. at [Source: (PushbackInputStream); line: 1, column: 17] (through reference chain: com.example.demo.uploaddto["filetoupload"])]
  70. **欢迎任何建议**
英文:

I am trying to upload multiple files and I am facing an issue while doing so. Can someone please suggest what is wrong?

I am enclosing relevant snippets of my code to debug better.

html code

  1. &lt;label&gt;
  2. welcome {{name}}, welcome to new app.
  3. &lt;/label&gt;
  4. &lt;div&gt;
  5. &lt;input type=&quot;file&quot; multiple placeholder=&quot;Select Files to be upload&quot; accept=&quot;.xlsx&quot; (change)=selectedfiles($event)&gt;
  6. &lt;/div&gt;

upload logic

  1. selectedfiles(event){
  2. this.selectedxlfiles=event.target.files;
  3. this.fileandinstancekeyobj.filetoupload=this.selectedxlfiles;
  4. this.fileandinstancekeyobj.instancekey=this.instancekey;
  5. this.uploadservice.uploadtoserver(this.fileandinstancekeyobj).subscribe(result=&gt;{
  6. console.log(result);
  7. })
  8. }

uploadservice

  1. uploadtoserver(selectedfileandinstacekeyobj): Observable&lt;HttpEvent&lt;{}&gt;&gt;{
  2. let url:string=environment.url+&#39;uploadfile&#39;;
  3. const newrequest=new HttpRequest(&#39;POST&#39;,url,selectedfileandinstacekeyobj,{
  4. reportProgress:true,
  5. responseType:&#39;text&#39;
  6. });
  7. return this.http.request(newrequest);
  8. }

springboot controller

  1. @RestController
  2. public class uploadcontroller {
  3. @PostMapping(&quot;/uploadfile&quot;)
  4. public ResponseEntity&lt;String&gt; handleupload(@RequestBody uploaddto dto){
  5. System.out.println(&quot;sucessfull&quot;);
  6. System.out.println(dto.getInstancekey()+&quot; &quot;+dto.getFiletoupload().length);
  7. return ResponseEntity.status(HttpStatus.OK).body(&quot;ok&quot;);
  8. }

upload DTO

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import org.springframework.web.multipart.MultipartFile;
  5. class uploaddto {
  6. List&lt;MultipartFile&gt; filetoupload;
  7. String instancekey;
  8. public uploaddto(List&lt;MultipartFile&gt; filetoupload, String instancekey) {
  9. super();
  10. filetoupload=new ArrayList&lt;MultipartFile&gt;();
  11. this.filetoupload = filetoupload;
  12. this.instancekey = instancekey;
  13. }
  14. public List&lt;MultipartFile&gt; getFiletoupload() {
  15. return filetoupload;
  16. }
  17. public void setFiletoupload(List&lt;MultipartFile&gt; filetoupload) {
  18. this.filetoupload = filetoupload;
  19. }
  20. public String getInstancekey() {
  21. return instancekey;
  22. }
  23. public void setInstancekey(String instancekey) {
  24. this.instancekey = instancekey;
  25. }
  26. }

I am receiving the following error

  1. [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
  2. Cannot deserialize instance of `java.util.ArrayList&lt;org.springframework.web.multipart.MultipartFile&gt;` out of START_OBJECT token;
  3. nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
  4. Cannot deserialize instance of `java.util.ArrayList&lt;org.springframework.web.multipart.MultipartFile&gt;` out of START_OBJECT token
  5. at [Source: (PushbackInputStream); line: 1, column: 17] (through reference chain: com.example.demo.uploaddto[&quot;filetoupload&quot;])]

Any suggestion would be appreciated

答案1

得分: 1

以下是您要翻译的内容:

我正在添加这个答案,以便我可以帮助某人挽救他的一天,我所做的更改如下:

在 UploadController 中的更改

  1. this.selectedxlfiles = event.target.files;
  2. const data: FormData = new FormData();
  3. for (let i = 0; i < this.selectedxlfiles.length; i++) {
  4. this.currentfile = this.selectedxlfiles[i];
  5. data.append('selectedfile', this.currentfile);
  6. }
  7. data.append('instancekey', this.instancekey);
  8. this.uploadservice.uploadtoserver(data).subscribe(Response => {
  9. console.log(Response);
  10. })

在 UploadService 中的更改

  1. uploadtoserver(data: FormData): Observable<HttpEvent<{}>> {
  2. let url: string = environment.url + 'uploadfile';
  3. const newrequest = new HttpRequest('POST', url, data, {
  4. reportProgress: true,
  5. responseType: 'text',
  6. });
  7. return this.http.request(newrequest);
  8. }

在 Spring Boot 控制器中的更改

  1. @RestController
  2. public class UploadController {
  3. @PostMapping("/uploadfile")
  4. public ResponseEntity<String> handleUpload(@ModelAttribute UploadDto dto) {
  5. System.out.println("成功");
  6. System.out.println(dto.getInstanceKey() + " " + dto.getFileToUpload().length);
  7. return ResponseEntity.status(HttpStatus.OK).body("ok");
  8. }
  9. }

控制器中唯一的更改是 @ModelAttribute

英文:

I am adding this answer so that i could help someone save his day, what i did

change in uploadcontroller

  1. this.selectedxlfiles=event.target.files;
  2. const data:FormData=new FormData();
  3. for(let i=0;i&lt;this.selectedxlfiles.length;i++){
  4. this.currentfile=this.selectedxlfiles[i];
  5. data.append(&#39;selectedfile&#39;,this.currentfile);
  6. }
  7. data.append(&#39;instancekey&#39;,this.instancekey);
  8. this.uploadservice.uploadtoserver(data).subscribe(Response=&gt;{
  9. console.log(Response);
  10. })

changes in upload service

  1. uploadtoserver(data:FormData): Observable&lt;HttpEvent&lt;{}&gt;&gt;{
  2. let url:string=environment.url+&#39;uploadfile&#39;;
  3. // console.log(url);
  4. // const data: FormData=new FormData();
  5. // data.append(&#39;selectedfile&#39;,selectedfile);
  6. // data.append(&#39;instancekey&#39;,instancekey);
  7. const newrequest=new HttpRequest(&#39;POST&#39;,url,data,{
  8. reportProgress: true,
  9. responseType: &#39;text&#39;,
  10. });
  11. return this.http.request(newrequest);
  12. //return this.http.post(url,selectedfiles);
  13. }

changes in springboot controller

  1. @RestController
  2. public class uploadcontroller {
  3. @PostMapping(&quot;/uploadfile&quot;)
  4. public ResponseEntity&lt;String&gt; handleupload(@ModelAttribute uploaddto dto){
  5. System.out.println(&quot;sucessfull&quot;);
  6. System.out.println(dto.getInstancekey()+&quot; &quot;+dto.getFiletoupload().length);
  7. return ResponseEntity.status(HttpStatus.OK).body(&quot;ok&quot;);
  8. }

the only change in controller @modelattribute

huangapple
  • 本文由 发表于 2020年9月21日 15:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63987945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定