SpringMVC form表单 上传一个文件

前台.jsp文件



 
    


    
    
    

后台控制器.java文件

import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.springframework.web.multipart.MultipartFile;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@RequiresPermissions("emergencyNotice:create")
@RequestMapping("/emergencyNotice/create")
public void createEmergencyNotice(EmergencyNotice emergencyNotice, MultipartFile file,
		HttpSession session) throws IOException {
       
     /*1处理实体类对象emergencyNotice,省略*/

     /*2处理文件*/
	String basePath = session.getServletContext().getRealPath("/"); // 获取基本路径

    /*如果文件不为空,保存文件*/ 
    if (file != null && !file.isEmpty()) {
	   String urlPath = HandleFile.saveFile(file, basePath); /*保存附件到本地*/ 

     } /*end if*/
}

这篇文章把提交文件和提交一个实体类(基本类型)放在了一起,但是对实体类的处理,省略了。想提醒一下:文件是可以和实体类(基本类型)一起提交到后台

这样提交,无论前台有没有提交文件,都不会出错的

HandleFile 中具体怎么保存文件的,参考
http://blog.csdn.net/dreamstar613/article/details/53841438

你可能感兴趣的:(前后台交互-form,spring-mvc)