Mengenai metode mengunggah gambar di SpringMVC, editor telah menyusun dua metode untuk Anda, konten spesifiknya adalah sebagai berikut:
Tipe pertama: (ditempatkan di lokasi yang sesuai dari alamat fisik di bawah proyek)
A. Metode Penulisan Jalur:
String basepath = "/web-inf/sumber daya/unggah";
String filePathName = request.getSession (). GetserVletContext (). GetRealPath (Basepath);
B. Jalur yang sebenarnya:
D: /workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/xyt/web-inf/resources/upload/image nama
C. Path Access: http: // localhost: 8080/xyt/sumber daya/unggah/nama gambar
D. Prasyarat: Selama proyek ini dapat berjalan.
Tipe kedua: (Buat jalur virtual, konfigurasikan server.xml di bawah tomcat, buat jalur penyimpanan dan jalur akses)
1. Penulisan jalur:
String filepathname = constant.img_path+file.separator+"unggah";
Di mana: string final statis publik img_path = "e: // java // img";
2. Konfigurasi jalur:
Konfigurasi server.xml
<Host name = "localhost" appbase = "webapps" unpackwars = "true" autodeploy = "true"> <valve classname = "org.apache.catalina.valves.accesslogvalve" direktori = "log" prefix = "localhost_access_log." suffix = ". txt" pola = " %h %l %u %t" %r " %s %b"/> <!-add (simpan gambar)-> <context path = "/unggah" docbase = "e:/java/img/unggah"> </konteks> </host>
3. Jalur aktual: e:/java/img/unggah
4. Path Access: http: // localhost: 8080/unggah/nama gambar
5. Referensi: http://my.oschina.net/pingdy/blog/381001
6. Prasyarat: Server Tomcat harus dibuka
Contoh: Contoh mengunggah gambar: (Beberapa gambar dapat diunggah)
JsonObject rs = new jsonobject (); commonsmultipartresolver multipartresolver = new commonsmultipartresolver (request.getSession (). GetServletContext ()); string url = ""; if (multipartresolver.ismultipart (request)) {Multiparthtracs) MultipARTRECERVEST (MultipARTRQUEST (permintaan))) {MultipARTHREFTPROR) permintaan; 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 ("//."); = f [f.length-1]; System.out.println (ext);} System.out.println (AllowImgType == null); if (! AllowimgType.contains (ext.touppercase ()) {rs.put ("kode", "err_upload_uPpercase (); rs.put (" r.put ("code", "err_upload_uPpercase"); Basepath = "/web-inf/sumber daya/unggah"; // string filepathname = request.getSession (). getServletContext (). getRealPath (Basepath); string filepathname = constant.img_path+file.separator+"unggah"; url = filoshname; System.out. File localfile = file baru (filepathname); if (! localfile.exists ()) {localfile.mkdir (); } // compressstring fname = new date (). getTime () + "." + ext; string originalFname = fname.substring (0, fname.indexof (".")) + "_ asli." + Ext; string fileName = filePathname + file.separator + fname; string ofilenePathNAME = filePathName + file.separator + fname; string ofilenePathName = filePathName. File baru (nama file); file ofile = file baru (Ofilename); try{ImageHelper.compress(file.getInputStream(), 600, infile);file.transferTo(oFile);//original Upload the original image JSONObject obj = new JSONObject();rs.put("code", Constant.CODE_SUCCESS);rs.put("data", obj.toString());}catch(Exception e) {rs.put ("kode", "err_upload_0001"); rs.put ("msg", "err_upload_0001"); e.printstacktrace (); return rs;}}Di atas terkait dengan mengunggah gambar dan mengakses SpringMVC, dan saya harap ini akan membantu semua orang.