我尝试使用spring mvc 4,spring boot和thymeleaf作为模板引擎上传多个文件,但我无法访问上传的文件,这些文件作为一个多部分文件处理内容类型application / octet-stream。这是我的前端代码:
<form name="offer-form" th:action="@{/submit-property}" method="POST" enctype="multipart/form-data">
<!-- .. other inputs .. -->
<div class="col-xs-12 margin-top-60">
<input id="file-upload"name="files[]" type="file" multiple="multiple"/>
</div>
<div class="col-xs-12">
<div class="center-button-cont margin-top-60">
<button type="submit" class="button-primary button-shadow">
<span>submit property</span>
<div class="button-triangle"></div>
<div class="button-triangle2"></div>
<div class="button-icon"><i class="fa fa-lg fa-home"></i></div>
</button>
</div>
</div>
</form>
控制器代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.aq.domain.Property;
import com.aq.service.AddPropertyFormDataInitializerService;
@Controller
public class PorpertySubmissionController
{
public static final Resource PICTURES_DIR = new FileSystemResource("./uploadedPictures");
//some unrelated code
@RequestMapping(value="/submit-property", method=RequestMethod.GET)
public String getSubmitPropertyForm(Model model)
{
model.addAttribute("property", new Property());
return "submit-property";
}
@RequestMapping(value="/submit-property", method=RequestMethod.POST)
public String submitProperty
(
@ModelAttribute(value="property") Property property,
@RequestParam("files[]") MultipartFile[] uploadedImages
)
{
//some unrelated code
if(uploadedImages != null && uploadedImages.length > 0)
{
System.out.println("uploadedImages length: " + uploadedImages.length);
for(MultipartFile imageFile : uploadedImages)
{
try
{
copyFileToPictures(imageFile);
System.out.println("copied File: " + imageFile.getOriginalFilename() + " successfully.");
}
catch (IOException e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
else
{
System.out.println("no images were uploaded");
}
return "redirect:/submit-property";
}
private Resource copyFileToPictures(MultipartFile file) throws IOException {
System.out.println("File Original Name: " + file.getOriginalFilename());
System.out.println("File Name: " + file.getName());
System.out.println("File size: " + file.getSize());
System.out.println("File Content type: " + file.getContentType());
String fileExtension = getFileExtension(file.getOriginalFilename());
File tempFile = File.createTempFile("pic", fileExtension, PICTURES_DIR.getFile());
try (InputStream in = file.getInputStream();
OutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return new FileSystemResource(tempFile);
}
private static String getFileExtension(String name) {
return name.substring(name.lastIndexOf("."));
}
}
sysout的输出:
uploadedImages长度:1(即使我上传了多个文件)
文件原始名称(使用getOriginalFileName):
文件名(使用getName):files []
文件大小:0
文件内容类型:application / octet-stream
然后在空原始文件名上调用subsctring时出现异常。
我尝试将公共文件上传添加到我的POM并配置了CommonsMultipartResolver bean,但是它总是打印出没有上传的图像(这意味着它的空值或长度= 0)
答案 0 :(得分:0)
如果有人面临同样的问题,事实证明它与春天和百里香无关。这只是因为我正在使用的主题使用bootstrap fileinput插件,它以两种模式工作:ajax和表单提交。事实证明,主题在文件上传中使用的大多数功能在表单提交模式下都不可用。所以这是第三方的lib问题,但我想我应该分享。