我一直在將Spring集成到一個應用程序中,並且必須從表單重做文件上傳。 我知道Spring MVC必須提供什麼,以及我需要做什麼來配置我的控制器以便能夠上傳文件。我已經閱讀了足夠的教程來做到這一點,但是這些教程沒有解釋的是正確/最佳實踐方法,以確定如何在實際處理文件後執行/如何完成該操作。下面是一些類似的代碼在Spring MVC的文檔中發現的處理上傳文件的代碼可在
Spring MVC File UploadSpring MVC文件上傳幫助
發現在下面你的例子中可以看到,他們告訴你什麼都做,以獲取該文件,但他們只是說做豆與東西
我已經檢查了許多教程,他們都似乎讓我到這一點,但我真正想知道的是處理該文件的最佳方式。一旦我有一個文件在這一點上,什麼是將這個文件保存到服務器上的目錄最好的方法是什麼?有人可以幫助我嗎?由於
public class FileUploadController extends SimpleFormController {
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws ServletException, IOException {
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
let's see if there's content there
byte[] file = bean.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
}
//do something with the bean
return super.onSubmit(request, response, command, errors);
}
只需簡單地打開輸出流並將字節寫入流。 FileOutputStram fos = new FileOutputStream(「location/on/server/filename」); fos.write(file); fos.close(); – mhshams 2010-08-26 18:00:48
你確實意識到你正在關注Spring 2.0的文檔,對吧?從那以後,事情在春天的世界裏發生了很多變化。我強烈建議使用3.0代替,你會發現許多事情更容易,包括文件上傳。 – skaffman 2010-08-26 18:48:42
我已經閱讀了Spring 3.0的文檔以及有關使用多部分表單的文檔,並且多部分處理的文檔與2.0文檔幾乎完全相同。 – TheJediCowboy 2010-08-26 20:18:09