springmvc利用jquery.form插件异步上传文件

需要的下载文件:

jquery.form.js

jquery.js

commons-fileupload.jar

commons-io.jar

示例图片

springmvc利用jquery.form插件异步上传文件_第1张图片

pom.xml


		
			commons-fileupload
			commons-fileupload
			1.3
		
		
			commons-io
			commons-io
			2.4
		


web.xml 解决上传后中文文件名乱码问题


	  
		 Set Character Encoding  
		 org.springframework.web.filter.CharacterEncodingFilter  
		   
		   	 encoding  
		   	 UTF-8  
	 	    
	  
	  
	   Set Character Encoding  
	   /*  
	 
	


servlet-context.xml中添加对上传的支持


	
	


jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path;
%>


	
	springmvc上传文件
	
	


	

springmvc上传文件






java的controller文件

package com.paincupid.springmvc.widget.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.paincupid.springmvc.finance.domain.Finance;
import com.paincupid.springmvc.test.domain.Person;
import com.paincupid.springmvc.util.BaseJsonRst;
import com.paincupid.springmvc.util.CreatMockData;

/**
 * 
 * @author arthur.paincupid.lee
 * @since 2016.01.24
 *
 */
@Controller
@RequestMapping("/widget")
public class widgetController {
	private static final Logger log = LoggerFactory.getLogger(widgetController.class);
	
	/**
	 * 上传文件
	 * 在前台的访问路径为: http://localhost:8080/springmvc/widget/uploadFile/view
	 * @return
	 */
	@RequestMapping("/uploadFile/view")
	public String uploadFile() {
		return "widget/uploadFile";
	}
	
	@ResponseBody
	@RequestMapping(value="/saveFile", method=RequestMethod.POST)
	public BaseJsonRst saveFile(@RequestParam MultipartFile file, 
			@RequestParam String projectName) {
		BaseJsonRst view = new BaseJsonRst();
		String orgiginalFileName = "";
		try {
			String fileName = file.getName();
			InputStream inputStream = file.getInputStream();
			String content = file.getContentType();
			orgiginalFileName = file.getOriginalFilename();
			log.info("fileName: "+fileName+", inputStream: "+ inputStream
						+"\r\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName
						+"\r\n projectName: "+ projectName);	
		} catch (IOException e) {
			e.printStackTrace();
		}
		view.setSuccess(true);
		view.setMessage("上传: "+orgiginalFileName+" 文件成功!");
		return view;
	}
	
	
	
	
	
	
	
	
}


访问路径:http://localhost:8080/springmvc/widget/uploadFile/view


下载源码地址:http://git.oschina.net/paincupid/springmvc/tree/bootstrap


转载请注明出处:http://blog.csdn.net/paincupid/article/details/50934222


你可能感兴趣的:(Spring)