お世話になります。
Spring Bootにてファイルアップロードを行いたいのですが以下の状況にて、うまくいくケースがほとんどなのですが、うまくいかない場合があるのでご教授頂きたいです。(10回に1回くらい失敗します。)
内容としては、下記コントローラーにて、getterでMultiPartFileオブジェクトを取得し、サイズや名前を確認してもサイズが0、名前は取得できない状況にあります。うまくいくケースでは当然、それぞれサイズや名前は取得できます。
よろしくお願いいたします。
2017/05/29追記
サーバーサイドを疑ってずっとデバッグしていましたが、フロント側でデバッグしてみたところ、フォーム送信直前のフォームオブジェクトがアップロードしたファイルを保持できていませんでした。発生頻度については、前述のとおりです。Formタグには、enctype="multipart/form-data"を指定しておりますが、そのほかにも何かフロント側で実施するべきことがあるのでしょうか?
html
1 2<form th:action="@{/registerEvidence}" method="post" enctype="multipart/form-data"> 3 <input type="file" name="evidence_file"/> 4 <input type="submit" value="送信" /> 5</form>
Java
1//Formエンティティ 2import org.springframework.web.multipart.MultipartFile; 3import lombok.Data; 4@Data 5public class EvidenceForm { 6 // アップロードファイル 7 private MultipartFile evidence_file; 8}
Java
1 // コントローラー 2 @RequestMapping(value="/registerEvidence" ,method=RequestMethod.POST) 3 public String registerEvidence( 4 @Validated @ModelAttribute("EvidenceForm") EvidenceForm evidenceForm, 5 BindingResult bindingResult, 6 Model model, 7 RedirectAttributes redirectAttributes 8 ) throws IOException 9 MultipartFile file = evidenceForm.getEvidence_file(); 10 Long size = file.getSize(); //失敗時はsizeが0になる 11 String name = file.getName(); //失敗時は空文字になる 12 13 if(size > 0){ 14 //登録処理 15 }
gradle
1dependencies { 2 compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" 3 compile('org.springframework.boot:spring-boot-starter-thymeleaf') 4 compile("org.springframework.boot:spring-boot-starter-data-jpa") 5 compile "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion" 6 compile("org.webjars:jquery:2.1.4") 7 compile("org.webjars:bootstrap:3.3.5") 8 compile "mysql:mysql-connector-java:$mySQLVersion" 9 compile("org.springframework.boot:spring-boot-devtools") 10 compile 'commons-codec:commons-codec:1.10' 11 12 compile("org.springframework.boot:spring-boot-starter-security") 13 compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4") 14 15 providedRuntime('org.projectlombok:lombok:1.16.6') 16 providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 17 testCompile('org.springframework.boot:spring-boot-starter-test') 18 19 compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") 20 compile("org.lazyluke:log4jdbc-remix:0.2.7") 21 compile("org.springframework.boot:spring-boot-configuration-processor") 22 compile("org.apache.commons:commons-lang3:3.0") 23 24 compile("org.apache.poi:poi:3.15") 25 compile("org.apache.poi:poi-ooxml:3.15") 26 compile("org.apache.poi:poi-ooxml-schemas:3.15") 27 28 compile("org.apache.commons:commons-collections4:4.1") 29 compile('commons-fileupload:commons-fileupload:1.2') 30 compile 'javax.mail:mail:1.4' 31 compile 'net.sf.opencsv:opencsv:2.3' 32 compile 'net.sf.jasperreports:jasperreports:6.2.0' 33}
SpringBoot 1.4.1
Jdk 1.8.0_121
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/29 00:13