在Spring MVC框架下利用Servlet3.0 API實現檔案上傳
阿新 • • 發佈:2019-02-01
Servlet3.0提供了對檔案上傳的原生支援,不需藉助任何第三方元件,直接使用Servlet3.0提供的API就能夠實現檔案上傳功能。本文使用IDEA IDE工具,在Spring MVC框架下,基於註解對映和資料繫結編寫Java業務程式碼,利用Servlet3.0 API來實現單個檔案的上傳示例。
部署配置檔案
MultipartConfig Servlet
在部署描述符中的DispatcherServlet中宣告賦值,使其Servlet變成MultipartConfig Servlet,具體新增以下標籤內容:
//web.xml
<servlet>
<servlet-name >springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
< /init-param>
<load-on-startup>2</load-on-startup>
<multipart-config>
<max-file-size>1000000</max-file-size>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern >
</servlet-mapping>
新增原因:Spring MVC無法直接對Servlet進行註解。另一種方法是用@MultipartConfig給DispatcherServlet進行註解,效果一樣。
MultipartResolver
在Spring MVC配置檔案中新增一個不同的多部分解析器,具體新增以下內容:
//springmvc.servlet.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
編寫Java程式碼
Domain類
/**
* UploadFile.java
*
* @author Zero
*/
public class UploadFile implements Serializable {
private static final long serialVersionUID = 123456789L;
private MultipartFile multipartFile;
private String description;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public MultipartFile getMultipartFile() {
return multipartFile;
}
public void setMultipartFile(MultipartFile multipartFile) {
this.multipartFile = multipartFile;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Service介面與實現類
/**
* MyFileService.java
*
* @author Zero
*/
public interface MyFileService {
//MyFileDto loadFileByGuid(String guid);
String uploadFile(UploadFile uploadFile, String realPath);
}
//MyFileServiceImpl.java
@Service("myFileService")
public class MyFileServiceImpl implements MyFileService {
private static final String UPLOAD_ERROR = "檔案上傳出錯!";
private static final String UPLOAD_SUCCESS = "檔案上傳成功!";
@Override
public String uploadFile(UploadFile uploadFile, String realPath) {
boolean loadFileVerify = false;
MultipartFile multipartFile = uploadFile.getMultipartFile();
String fileName = multipartFile.getOriginalFilename();
File file = new File(realPath, fileName);
try {
multipartFile.transferTo(file);
loadFileVerify = true;
} catch (IOException e) {
e.printStackTrace();
}
return loadFileVerify ? UPLOAD_SUCCESS : UPLOAD_ERROR;
}
}
Controller類
/**
* MyFileController.java
*
* @author Zero
*/
@Controller
@RequestMapping("/public/")
public class MyFileController {
private static Logger LOG = LoggerFactory.getLogger(FileController.class);
private static final String UPLOAD_EMPTY = "檔案為空!";
@Autowired
private MyFileService fileService;
@RequestMapping(value = "file_upload", method = RequestMethod.POST)
public String upload(@ModelAttribute(uploadFile) @Valid UploadFile uploadFile, BindingResult result, HttpServletRequest request, Model model) {
if (result.hasErrors()) {
LOG.warn(UPLOAD_EMPTY);
model.addAttribute("uploadResult", UPLOAD_EMPTY);
return "index";
}
String uploadResult = fileService.uploadFile(uploadFile, request.getServletContext().getRealPath("/file"));
model.addAttribute("uploadResult", uploadResult);
return "index";
}
}
編寫前端頁面程式碼
編寫JSP測試表單
//index.jsp
<form action="public/file_upload" method="post" enctype="multipart/form-data">
<h4>File Upload</h4>
<div class="form-group">
<label for="multipartFile">File select:</label>
<input type="file" name="multipartFile" id="multipartFile"/>
</div>
<div class="form-group">
<label for="description">File description:</label>
<input type="text" name="description" id="description"/>
</div>
<div class="form-group">
<input type="submit" value="upload"/>
<p >${uploadResult}</p>
</div>
</form>
注意事項
1.若BindingResult返回false,可能是domain類引數匹配不對,特別是serialVersionUID,需用idea自生成;
2.若表單post提交的multipartFile資料無法繫結到後臺對應屬性,可能是表單配置不對enctype=”multipart/form-data”。