關于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配置
1
2
3
4
5
6
7
8
|
<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服務器
舉例:上傳圖片的實例:(可以上傳多張圖片)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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( "\\." ); 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(); } //compress String 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上傳圖片與訪問的相關內容,希望對大家有所幫助。