采用jquery.form.js異步上傳圖片,並結合<form>表單
<script type="text/javascript"> //采用jquery.form.js異步上傳圖片,並結合<form>表單 function uploadPicture() { var options = { //請求路徑 url : "/upload/uploadPic.do", dataType : "json", type : "post", success : function(data) { //處理結果 //將相對路徑設置給隱藏域中,提交時用 $("#imgUrl").val(data.path); //將全路徑設置給img標簽,顯示圖片用 $("#allImgUrl").attr("src", data.url); } } $("#jvForm").ajaxSubmit(options); } </script> <tr> <td width="20%" class="pn-flabel pn-flabel-h"></td> <td width="80%" class="pn-fcontent"> <img width="100" height="100" id="allImgUrl" /> <!-- 圖片存在數據庫的路徑 --> <input type="hidden" id="imgUrl" name="imgUrl"></input> <input type="file" onchange="uploadPicture()" name="uploadPic" /></td> </tr>
一定要在form表單中填寫enctype="multipart/form-data"
<form id="jvForm" action="/brand/add.do" method="post" enctype="multipart/form-data"> </form>
在springmvc.xml文件中添加文件軟件器
<!-- 圖片轉換器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean>
編寫文件上傳UploadController.java
1 package cn.lzc.code.controller.admin; 2 3 import java.io.IOException; 4 import java.text.DateFormat; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.Random; 8 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.apache.commons.io.FilenameUtils; 12 import org.json.JSONObject; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestMethod; 16 import org.springframework.web.bind.annotation.RequestParam; 17 import org.springframework.web.multipart.MultipartFile; 18 19 import com.sun.jersey.api.client.Client; 20 import com.sun.jersey.api.client.ClientHandlerException; 21 import com.sun.jersey.api.client.UniformInterfaceException; 22 import com.sun.jersey.api.client.WebResource; 23 24 import cn.lzc.code.web.Constants; 25 import cn.lzc.common.web.ResponseUtils; 26 27 /** 28 * 上傳文件管理Controller 29 * 30 * @author admin 31 * 32 */ 33 @Controller 34 public class UploadController { 35 36 /** 37 * 異步上傳圖片 38 * 39 * @param uploadFile 40 * @param response 41 */ 42 @RequestMapping(value="/upload/uploadPic.do",method=RequestMethod.POST) 43 public void uploadBrandPic(@RequestParam(required = false) MultipartFile uploadPic, HttpServletResponse response) { 44 // 圖片名稱生成策略---采用時間格式(精確到毫秒)並追加隨機3位(10以內)數字 45 // 精確到毫秒 46 DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 47 String picName = df.format(new Date()); 48 //隨機再生成3位10以內的數 49 Random r=new Random(); 50 for (int i = 0; i < 3; i++) { 51 picName+=r.nextInt(10); 52 } 53 //獲取擴展名 54 String originalFilename = uploadPic.getOriginalFilename(); 55 String ext = FilenameUtils.getExtension(originalFilename); 56 //相對路徑 57 String path="upload/"+picName+"."+ext; 58 //全路徑 59 String url="http://localhost:8088/image-web/"+path; 60 61 //jersey發送另一台Tomcat(可讀寫) 62 // 實例化Jersey 63 Client client=new Client(); 64 //想要發送到的服務器地址,記住,必須設置tomcat服務器的權限,不然無法上傳到tomcat 65 //設置請求路徑 66 WebResource resource = client.resource(url); 67 try { 68 //發送開始put 69 resource.put(String.class, uploadPic.getBytes()); 70 } catch (UniformInterfaceException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } catch (ClientHandlerException e) { 74 // TODO Auto-generated catch block 75 e.printStackTrace(); 76 } catch (IOException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } 80 //返回json數據給頁面,(包括url回顯路徑,Path保存數據庫的路徑) 81 JSONObject jo=new JSONObject(); 82 jo.put("path", path); 83 jo.put("url", url); 84 //返回數據給頁面 85 ResponseUtils.renderJson(response, jo.toString()); 86 } 87 }
寫一個RequestUtils.java工具類,用來響應相應的數據到前台頁面
1 package cn.lzc.common.web; 2 3 import java.io.IOException; 4 5 import javax.servlet.http.HttpServletResponse; 6 7 /** 8 * Response幫助類 支持JSON XML Text 9 * 10 * @author admin 11 * 12 */ 13 14 public class ResponseUtils { 15 // 發送Json 16 public static void renderJson(HttpServletResponse response, String text) { 17 rend(response, "application/json;charset=UTF-8", text); 18 } 19 20 // 發送xml 21 public static void renderXml(HttpServletResponse response, String text) { 22 rend(response, "text/xml;charset=UTF-8", text); 23 } 24 25 // 發送text 26 public static void renderText(HttpServletResponse response, String text) { 27 rend(response, "text/plain;charset=UTF-8", text); 28 } 29 30 // 發送 31 public static void rend(HttpServletResponse response, String contextType, String text) { 32 // 設置傳輸類型 33 response.setContentType(contextType); 34 // 發送 35 try { 36 response.getWriter().write(text); 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 }
