📅  最后修改于: 2020-12-04 08:41:52             🧑  作者: Mango
Spring MVC提供了一种上传文件的简便方法,它可以是图像或其他文件。让我们看一个使用Spring MVC上传文件的简单示例。
要运行此示例,您需要加载:
1)下载spring的所有jar文件,包括core,web,aop,mvc,j2ee,remoting,oxm,jdbc,orm等。
1)添加commons-io和fileupload.jar文件
2)在spring-servlet.xml中添加CommonsMultipartResolver条目
3)创建表格以提交文件。方法名称必须为“ post”,并键入“ multiple / form-data”。
4)在Controller中使用CommonsMultipartFile类。
@RequestMapping(value="/savefile",method=RequestMethod.POST)
public ModelAndView upload(@RequestParam CommonsMultipartFile file,HttpSession session){
String path=session.getServletContext().getRealPath("/");
String filename=file.getOriginalFilename();
System.out.println(path+" "+filename);
try{
byte barr[]=file.getBytes();
BufferedOutputStream bout=new BufferedOutputStream(
new FileOutputStream(path+"/"+filename));
bout.write(barr);
bout.flush();
bout.close();
}catch(Exception e){System.out.println(e);}
return new ModelAndView("upload-success","filename",path+"/"+filename);
}
5)在JSP中显示图像。
Upload Success
在您的项目中创建“ images”目录,因为我们正在编写代码以将所有文件保存在“ / images”目录中。
package com.javatpoint;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
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.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
private static final String UPLOAD_DIRECTORY ="/images";
@RequestMapping("uploadform")
public ModelAndView uploadForm(){
return new ModelAndView("uploadform");
}
@RequestMapping(value="savefile",method=RequestMethod.POST)
public ModelAndView saveimage( @RequestParam CommonsMultipartFile file,
HttpSession session) throws Exception{
ServletContext context = session.getServletContext();
String path = context.getRealPath(UPLOAD_DIRECTORY);
String filename = file.getOriginalFilename();
System.out.println(path+" "+filename);
byte[] bytes = file.getBytes();
BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(
new File(path + File.separator + filename)));
stream.write(bytes);
stream.flush();
stream.close();
return new ModelAndView("uploadform","filesuccess","File successfully saved!");
}
}
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
在这里,您需要为CommonsMultipartResolver创建一个bean。
这里的表单必须是method =“ post”和enctype =“ multipart / form-data”。
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
Image File Upload
File Upload Example - JavaTpoint
${filesuccess}
转到服务器控制台上打印的路径,以查看上载的文件。
我们已经在MyEclipse IDE中创建了该应用程序,该应用程序已经提供了jar文件。如果您使用eclipse或其他IDE,则需要为spring MVC加载jar文件。