Spring MVC文件上传帮助 [英] Spring MVC File Upload Help
问题描述
我知道Spring MVC必须提供什么,我需要做什么来配置我的控制器才能够上传文件。我已经阅读了足够多的教程可以做到这一点,但是这些教程没有解释什么是正确的/最佳实践方法,如何处理文件,如何处理文件。下面是一些类似于Spring MVC文档中处理文件上传代码的代码,可以在http://bbs.bss.org/spring/docs/2.0找到。 x / reference / mvc.htmlrel =noreferrer> Spring MVC文件上传
在下面的示例中,您可以看到它们向您展示了一切做得到的文件,但他们只是说做一些与豆
我已经检查了许多教程,他们似乎都让我这一点,但我真正想知道的是处理文件的最好方法。一旦我有一个文件在这一点上,什么是最好的方式来保存这个文件到服务器上的目录?有人可以帮我吗?谢谢
public class FileUploadController extends SimpleFormController {
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse响应,
Object命令,
BindException错误)抛出ServletException,IOException {
//转换bean
FileUploadBean bean =(FileUploadBean)命令;
让我们看看是否有内容
byte [] file = bean.getFile();
if(file == null){
// hmm,这很奇怪,用户没有上传任何东西
}
//对bean做些什么
返回super.onSubmit(request,response,command,errors);
制作uploads.I认为让春天处理文件保存,是最好的办法。 Spring使用它的 MultipartFile.transferTo(File dest)
函数。 > import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping(/ upload)
public class UploadController {
@ResponseBody
@RequestMapping(value = / save)
public String handleUpload(
@RequestParam(value =file,required = false)MultipartFile multipartFile,
HttpServletResponse httpServletResponse){
String orgName = multipartFile.getOriginalFilename();
String filePath =/ my_uploads /+ orgName;
文件dest =新文件(filePath);
尝试{
multipartFile.transferTo(dest);
} catch(IllegalStateException e){
e.printStackTrace();
返回File uploaded failed:+ orgName;
} catch(IOException e){
e.printStackTrace();
返回File uploaded failed:+ orgName;
}
返回File uploaded:+ orgName;
}
}
I have been integrating spring into an application, and have to redo a file upload from forms.
I am aware of what Spring MVC has to offer and what I need to do to configure my controllers to be able to upload files. I have read enough tutorials to be able to do this, but what none of these tutorials explain is correct/best practice methods on how/what is to be done to actually handle the file once you have it. Below is some code similar to code found on the Spring MVC Docs on handling file uploads which can be found at
Spring MVC File Upload
In the example below you can see that they show you everything to do to get the file, but they just say Do Something with the bean
I have checked many tutorials and they all seem to get me to this point, but what I really want to know is the best way to handle the file. Once I have a file at this point, what is the best way to save this file to a directory on a server? Can somebody please help me with this? Thanks
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);
}
This is what i prefer while making uploads.I think letting spring to handle file saving, is the best way. Spring does it with its MultipartFile.transferTo(File dest)
function.
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/upload")
public class UploadController {
@ResponseBody
@RequestMapping(value = "/save")
public String handleUpload(
@RequestParam(value = "file", required = false) MultipartFile multipartFile,
HttpServletResponse httpServletResponse) {
String orgName = multipartFile.getOriginalFilename();
String filePath = "/my_uploads/" + orgName;
File dest = new File(filePath);
try {
multipartFile.transferTo(dest);
} catch (IllegalStateException e) {
e.printStackTrace();
return "File uploaded failed:" + orgName;
} catch (IOException e) {
e.printStackTrace();
return "File uploaded failed:" + orgName;
}
return "File uploaded:" + orgName;
}
}
这篇关于Spring MVC文件上传帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!