SpringMVC에서 사진을 업로드하는 방법과 관련하여 편집기는 두 가지 방법을 편집했습니다. 특정 컨텐츠는 다음과 같습니다.
첫 번째 유형 : (프로젝트에 따라 물리적 주소의 해당 위치에 배치)
에이. 경로 쓰기 방법 :
String BasePath = "/web-inf/resources/upload";
문자열 FilePathName = requestSession (). getServletContext (). getRealPath (BasePath);
비. 실제 경로 :
D :/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/xyt/web-inf/resources/upload/image 이름
기음. 액세스 경로 : http : // localhost : 8080/xyt/resources/upload/image name
디. 전제 조건 : 이 프로젝트가 실행될 수있는 한.
두 번째 유형 : (가상 경로 생성, Tomcat에서 Server.xml 구성, 스토리지 경로 및 액세스 경로 생성)
1. 경로 쓰기 :
문자열 FilePathName = constant.img_path+file.separator+"Upload";
여기서 : 공개 정적 최종 문자열 img_path = "e : // java // img";
2. 경로 구성 :
Server.xml 구성
<호스트 이름 = "localhost"appbase = "webapps"unpackwars = "true"autodeploy = "true"> <valve classname = "org.apache.catalina.valves.accesslogvalve"directory = "logs"prefix = "localhost_access_log." 접미어 = ". txt"pattern = " %h %l %u %t" %r " %s %b"/> <!-추가 (사진 저장)-> <context path = "/upload"docbase = "e :/java/img/upload"> </hostxt> </hastxt>
3. 실제 경로 : e :/java/img/upload
4. 액세스 경로 : http : // localhost : 8080/업로드/이미지 이름
5. 참조 : http://my.oschina.net/pingdy/blog/381001
6. 전제 조건 : Tomcat 서버를 열어야합니다
예 : 사진 업로드 예 : (여러 사진을 업로드 할 수 있음)
jsonObject rs = new JsonObject (); CommonSmultipartresolver multipartresolver = new CommonSmultipartresolver (request.getSession (). getServletContext ()); String URL = "" "; if (multipartRequestequest = (multiptarThquest). 요청; iterator <string> iter = multirequest.getFilenames (); while (iter.hasnext ()) {multipartFile 파일 = multirequest.getFile ((string) iter.next ()); if (file! = null) {String originalFilename = file.getoriginalFilename (); String [] f = originalFilename.split ( "//"); = f [f.length-1]; system.out.println (ext);} system.out.println (allowimgtype == null); if (! allowimgtype.contains (ext.toupperCase ()) {rs.put ( "code", "err_upload_0003"); BasePath = "/web-inf/resources/upload"; // string filepathName = requestSession (). getServletContext (). getEpath (basePath); String FilePathName = constant.img_path+file.separator+"URL = FilePathName; System.out.println (url); // exchert in the world. 파일 localfile = 새 파일 (FilePathName); if (! localfile.exists ()) {localfile.mkdir (); } // compressString fname = new Date (). getTime () + "." + ext; String; String OriginalFname = fname.subString (0, fname.indexof ( ".")) + "_ or file (filename); file ofile = new File (OfiLename); {imageHelper.compress (file.getInputStream (), 600, infile); file.transferto (ofile); // 원본 이미지를 업로드 jsonobject obj = new jsonobject (); rs.put ( "code", constant.code_success); rs.put ( "data", obj.toString ()} {r e)} "err_upload_0001"); rs.put ( "msg", "err_upload_0001"); e.printstacktrace (); return rs;}}위의 내용은 사진 업로드 및 SpringMVC 액세스와 관련이 있으며 모든 사람에게 도움이되기를 바랍니다.