我一直在将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