Spring Mvc 上传图片全部过程(一)

2014-11-24 07:32:12 · 作者: · 浏览: 0

我也是google和百度和很久一点一点弄的……,希望能帮助到你。

直接看代码吧……

     图片:  
  
 	  
 	  预览
 	  修改
     



  


  
图片路径: 图片大小不超过2M
init: function(uuid) { // this.identifier 是设定的全局变量,uuid是页面加载时的唯一编码 this.identifier = uuid; // 图片上传 var idf = this.identifier; var that = this; $('#'+idf+'-tforma').ajaxForm({ dataType : 'json', beforeSubmit : function(a, f, o) { $('#'+idf+'-statusPic').html('上传中...'); }, success : function(data) { if (typeof (data) == 'string') data = eva l('(' + data + ')'); $('#'+idf+'-uploadWindow').window('close'); if ("success" == data.message) { $('div[identifier='+that.identifier+']').find('#picPath').val(data.path); $("#"+idf+"-path").val(data.path); $("#"+idf+"-statusPic").html( "预览"); } else if ("error" == data.message) $("#"+idf+"-statusPic").html("非图片数据!"); else $("#"+idf+"-statusPic").html("上传数据错误!"); $("#"+idf+"-itemPic").val(''); }, error : function(jqXHR, textStatus,errorThrown) { $('#$'+idf+'-uploadWindow').window('close'); //console.log("error:"+ data.responseText); //console.log("status:" + textStatus); $("#"+idf+"-statusPic").html("上传失败!"); $("#"+idf+"-itemPic").val(''); } }); }
/**
	 * 商品上传指定的图片
	 * 
	 * @param request
	 * @param response
	 * @param command
	 * @return
	 * @throws Exception
	 */
	public ModelAndView uploadFile(HttpServletRequest request, HttpServletResponse response, SmartMenu command) throws Exception {
		PrintWriter writer = null;
		try {
			response.setContentType("application/json; charset=GBK");
			writer = response.getWriter(); 

			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			String source = request.getSession().getServletContext().getRealPath("/");
			// 获得第1张图片(根据前台的name名称得到上传的文件)
			MultipartFile imgFile1 = multipartRequest.getFile("itemPic");
			// 判断是否是重复上传图片(如果重复将删除旧图片)
			String sourceImg = request.getParameter("path");
			if (imgFile1.getContentType().split("/")[0].equals("image")) {
				if (null != sourceImg && !"".equals(sourceImg)) {
					System.out.println("旧图片路径:" + sourceImg);
					File f = new File(source + sourceImg);
					if (f.isFile()) {
						f.delete();
						System.out.println(" 删除成功");
					} else
						System.out.println(" 删除失败!");
				}
				String path = null;
				if (imgFile1 != null && imgFile1.getSize() > 0) {
					path = this.getSmartMenuService().storeFile(request.getSession(), imgFile1);
				}
				writer.print("{\"message\":\"success\",\"path\":\"" + path.replace("\\", "\\\\") + "\"}");
			} else
				writer.print("{\"message\":\"error\"}");
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("{\"message\":\"no\"}");
		}
		writer.flush();
		writer.close();
		return null; 
	}


	@Override
	public String storeFile(HttpSession session, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		String fileType = file.getContentType().split("/")[1];
		String path = session.getServletContext().getRealPath("/");
		String separator = File.separator;
		String uuid = UUID.randomUUID().toString();
		FileOutputStream fos = null;
		String fileName = null;
		try {
			InputStream fis = file.getInputStream();
			// 转换文件为png格式,并保存在同名目录下
			File files = new File(path + "\\dishpic");