国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

SpringMVC上傳圖片與訪問

2019-11-26 14:39:01
字體:
來源:轉載
供稿:網友

關于springmvc上傳圖片的方法小編給大家整理了兩種方法,具體內容如下所示:

第一種:(放在該項目下的物理地址對應的位置)

a. 路徑寫法:

String basePath="/WEB-INF/resources/upload";
String filePathName= request.getSession().getServletContext().getRealPath(basePath);存放路徑

b. 實際路徑:

D:/WorkSpace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/XYT/WEB-INF/resources/upload/圖片名稱

c. 訪問路徑: http://localhost:8080/XYT/resources/upload/圖片名稱

d. 前提:只要這個項目能運行就行。

第二種:(創建虛擬路徑,配置Tomcat下server.xml,創建存儲路徑和訪問路徑)

1.路徑寫法:

String filePathName=Constant.IMG_PATH+File.separator+"upload";

其中:public static final String IMG_PATH = "E://Java//img";

2.路徑配置:

Server.xml配置

<Host name="localhost" appBase="webapps"unpackWARs="true" autoDeploy="true"><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt"pattern="%h %l %u %t "%r" %s %b" /><!-- add(save pictures) --><Context path="/upload" docBase="E:/Java/img/upload"></Context></Host>

3.實際路徑:E:/Java/img/upload

4.訪問路徑:http://localhost:8080/upload/圖片名稱

5.參考:http://my.oschina.net/pingdy/blog/381001

6.前提:必須打開Tomcat服務器

舉例:上傳圖片的實例:(可以上傳多張圖片)

JSONObject rs=new JSONObject();CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());String url="";if (multipartResolver.isMultipart(request)) {MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;Iterator<String> iter = multiRequest.getFileNames();while (iter.hasNext()) {MultipartFile file = multiRequest.getFile((String) iter.next());if (file != null) {String originalFileName = file.getOriginalFilename();String[] f = originalFileName.split("http://.");String ext = "";if(f!=null && f.length>1){ext = f[f.length-1];System.out.println(ext);}System.out.println(allowImgType==null);if(!allowImgType.contains(ext.toUpperCase())){rs.put("code", "ERR_UPLOAD_0003");rs.put("msg", "類型錯誤");return rs;}//String basePath="/WEB-INF/resources/upload";//String filePathName = request.getSession().getServletContext().getRealPath(basePath);String filePathName=Constant.IMG_PATH+File.separator+"upload";url = filePathName;System.out.println(url);//上傳后記錄在path這個路徑下。File localFile = new File(filePathName);if(!localFile.exists()){ localFile.mkdir(); } //compressString fname =new Date().getTime()+"."+ext;String originalFname = fname.substring(0,fname.indexOf("."))+"_original."+ext;String fileName = filePathName + File.separator + fname;String oFileName = filePathName + File.separator + originalFname;File infile = new File(fileName);File oFile = new File(oFileName); try{ImageHelper.compress(file.getInputStream(), 600, infile);file.transferTo(oFile);//original 上傳原圖JSONObject obj = new JSONObject();rs.put("code", Constant.CODE_SUCCESS);rs.put("data", obj.toString());}catch(Exception e){rs.put("code", "ERR_UPLOAD_0001");rs.put("msg", "ERR_UPLOAD_0001");e.printStackTrace();return rs;}}

以上所述是針對SpringMVC上傳圖片與訪問的相關內容,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祥云县| 绵阳市| 临洮县| 迭部县| 万山特区| 贺兰县| 中牟县| 普陀区| 桂东县| 中方县| 什邡市| 天全县| 哈密市| 乌鲁木齐市| 灵寿县| 龙海市| 怀柔区| 湄潭县| 五家渠市| 渝中区| 岳西县| 商南县| 临澧县| 顺平县| 射洪县| 富川| 临江市| 蒙阴县| 万宁市| 阳信县| 赤峰市| 台安县| 登封市| 漠河县| 溧水县| 宕昌县| 景泰县| 同心县| 洱源县| 临颍县| 沈丘县|