1.图片服务器Tomcat的设置  tomcat/conf/web.xml中内容更改,将tomcat设置为可以读写

<servlet>
    <servlet-name>default</servlet-name>        
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
       <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>


2.前台页面提供上传图片的表单和ajax异步的逻辑

<form id="jvForm" action="add.do" method="post" enctype="multipart/form-data">
……
    <img src="" id="allImgUrl"/>
    <input type="hidden" name="imgUrl" id="imgUrl"/>
    <input type="file" name="uploadPic" onchange="uploadPic()"/>
</form>
<script type="text/javascript" scr="/res/common/js/jquery.form.js"></script>
<script type="text/javascript">
    //采用jquery.form.js异步上传图片,并结合<form>表单
    function uploadFic(){
        var options = {
            //请求路径
            url = "/upload/uploadPic.do",
            dataType = "json",
            type = "post",
            beforeSubmit : function(formData,jqForm,options){
                //判断是否为图片
                var f = jqForm[0];//将jqForm转成DOM对象
                var v = f.logoPic.value;//获取DOM对象中name为logoPic的值
                //获取扩展名,并转成小写
                var ext = v.substring(v.length-3).toLowerCase();
                //比对扩展名 jpg gif bmp png 
                if(ext != "jpg" && ext != "gif" && ext != "bmp" && ext != "png"){
                    alert("只允许上传图片!");
                    return false;
                }
                //校验提交的表单
                return true;
             },
            success : function(data){
               //处理结果
                //将相对路径设置给隐藏域中,提交时用
                $("#imgUrl").val(data.path);
                //将全路径设置给img标签,显示图片用
                $("#allImgUrl).attr("src",data.url);
           }
       }
     
           $("#jvForm").ajaxSubmit(options);
      }
</script>


3.springmvc-back.xml配置图片转换器

    <!-- 上传图片转换器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 设置上传文件的最大尺寸为1MB -->  
    <property name="maxUploadSize" value="1048576"/>
</bean>



4.UploadController上传图片到另一台服务器

需要的jar包如下

    commons-io-1.3.2.jar

    jersey-client-1.18.1.jar

    jersey-core-1.18.1.jar


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.itcast.core.web.Constants;
import cn.itcast.core.web.ResponseUtils;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

@Controller
public class UploadController {

	@RequestMapping(value="/upload/uploadPic.do")
	public void uploadPic(@RequestParam(required=false)MultipartFile pic,HttpServletResponse response){
		//@RequestParam(required=false)MultipartFile pic 接收前台表单name=pic文件,required=false表示文件可以为空
		//response 表示springmvc处理异步上传的返回值
	//设置图片到指定的服务器
		//文件扩展名
		String ext = FilenameUtils.getExtension(pic.getOriginalFilename());
		//图片名称=日期+随机数+图片原始名
		String format = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		Random r = new Random();
		for(int i=0;i<3;i++){
			format+=r.nextInt(10);
		}
	//Jersey工具包发图片到指定服务器
		//实例化Jersey
		Client client = new Client();
		//保存数据库的path
		String path = "/upload/"+format+"."+ext;
		//图片在图片服务器上的请求地址
		String url = Constants.IMAGE_URL+path;
		
		//jersey设置请求路径
		WebResource resource = client.resource(url);
		//jersey写数据
		try {
			resource.put(String.class,pic.getBytes());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//ajax回调函数接收的数据
		JSONObject jo = new JSONObject();
		jo.put("url", url);
		jo.put("path", path);
		ResponseUtils.renderJson(response, jo.toString());
	
	}
	


5.ajax回调数据格式

/**
 * 异步放回数据格式处理
 * @author pengya
 *
 */
public class ResponseUtils {

	//发送内容
	public static void render(HttpServletResponse response,String contentType,String text){
		response.setContentType(contentType);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//发送json
	public static void renderJson(HttpServletResponse response,String text){
		render(response,"application/json;charset=utf-8",text);
		
	}
	
	//发送xml
	public static void renderXml(HttpServletResponse response,String text){
		render(response, "text/xml;charset=UTF-8", text);
	}
	//发送text
	public static void renderText(HttpServletResponse response,String text){
		render(response, "text/plain;charset=UTF-8", text);
	}
	
}