บทความนี้แบ่งปันกรณีการอัปโหลดรูปภาพที่สมบูรณ์ใน Javaweb เพื่ออ้างอิง เนื้อหาเฉพาะมีดังนี้
แนวคิดการใช้งาน
•ใช้ปลั๊กอิน JCrop เพื่อตัดพิกัดของรูปภาพบนโทรศัพท์ของคุณ
•ส่งพารามิเตอร์ที่รวบรวมไปยังพื้นหลังใช้วัตถุกราฟิก Java เพื่อวาดภาพสำหรับการครอบตัด
◦การไหลของการประมวลผลด้านหลัง:
1. บีบอัดรูปภาพที่อัปโหลดตามสัดส่วนและอัปโหลดไปยังเซิร์ฟเวอร์ไฟล์และบันทึกรูปภาพที่บีบอัดในไดเรกทอรีชั่วคราวท้องถิ่น
2. สะท้อนภาพที่ถูกบีบอัดไปยังหน้าใช้ JCrop สำหรับการปลูกพืชและคลิปพิกัดบนโทรศัพท์ (x, y, ความกว้าง, ความสูง)
■@paramx เป้าหมายจุดเริ่มต้นพิกัด x
■@param y จุดเริ่มต้นจุดเริ่มต้นประสานงาน y
■@พารามิเตอร์ความกว้างเป้าหมายความกว้างของชิ้น■@พารามิเตอร์ความสูงเป้าหมายชิ้นส่วน
3. การประมวลผลหลังเวทีของการปลูกพืชและการอัปโหลด
หน้า JSP
< %@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> < %@ page contentType = "ข้อความ/html; charset = utf-8" metta htta html> content = "text/html; charset = utf-8"/> <title> อัปโหลด avatar ผู้ใช้ </title> <link href = "$ {pageContext.request.contextpath} /js/cutimg/css/master.css" rel = "stylesheet"/> <ลิงค์ href = "$ {pageContext.request.contextpath} /js/cutimg/css/upload.css" rel = "stylesheet"/> <link href = "$ {pageContext.request.context href = "$ {pageContext.request.contextpath} /js/cutimg/css/jquery.jcrop.css" rel = "Stylesheet"/> </head> <body> <div> <form id = "uploadimgfrom" enctype = "multipart/forta-data id = "pic2"> <div id = "uploadfile"> <div style = "margin-left: 75px;"> <a href = "JavaScript:;" id = "uptext" style = "สี: สีแดง;"> อัพโหลด avatar </a> </div> <p> รองรับรูปแบบ jpg และ png <br/> ภาพน้อยกว่า 2m </p> </div> <! <a href = "JavaScript:;" style = "สี: สีแดง"> upload อีกครั้ง </a> </p> <อินพุต type = "hidden" name = "picval" id = "picval" /> <อินพุต type = "hidden" name = "origpicval" id = "origpicval" value = " /> size = "100" /> <อินพุต type = "hidden" name = "fileUrl" id = "fileUrl" size = "100" /> <อินพุต type = "hidden" name = "imgheight" id = "imgheight" size = "100" /> <อินพุตประเภท = "hidden" src = "$ {pageContext.request.contextpath}/images/4-3.jpg"/> </div> <p> 400 x 300 พิกเซล </p> <%-<div> <img id = "preview_2" src = $ {pageContext.request.context.context พิกเซล </p>-%> <p> ลากหรือซูมเพื่อสร้างอวตารที่น่าพอใจ </p> </div> <%-<div id = "img_description"> <textarea </div>-%> <div> <font id = "upalert"> </font> <อินพุต type = "hidden" name = "w" id = "w" size = "5" /> <อินพุต type = "hidden" name = "h" id = "h" size = "5" /> <อินพุตประเภท = "hidden" name = "imgurl" id = "imgurl" /> < href = "JavaScript:;" style = "color: red"> บันทึก Avatar </a> </div> </div> </form> </div> </body> <script type = "text/javascript" src = "$ {pageContext.request.context.context} /js/jquery.form.js" src = "$ {pageContext.request.contextpath} /js/jquery.validate.min.js"> </script> <script type = "text/javascript" src = "$ {pageContext.request.context.context src = "$ {pageContext.request.contextpath} /js/check.js"> </script> <script src = "$ {pagecontext.request.contextpath} /js/cutimg/js/jquery-1.7.1.min.js src = "$ {pageContext.request.contextpath} /js/cutimg/js/jquery.form.js" type = "text/javascript"> </script> <script src = "$ {pagecontext.request.contextpath} /js/cutimg/js type = "text/javascript"> </script> <script src = "$ {pageContext.request.contextpath} /js/cutimg/js/jquery-custom-file-put.js" text/"javascript"> </script> src = "$ {pageContext.request.contextpath} /js/cutimg/js/jquery.jcrop.js" type = "text/javascript"> </script> <script src = "$ {pageContext.request.context.context.context type = "text/javascript"> </script> <script src = "$ {pageContext.request.contextpath} /js/cutimg/js/jquery.rollbar.min.js" type = "text/javascript"> </script> src = "$ {pageContext.request.contextpath} /js/cutimg/js/upload.ui.photos.js" type = "text/javascript"> </script> <script type = "text/javascript" src = "$ {pageContext.request.contextpath} /js/dialog/dialog.js?lib=false"> </script> <script type = "text/javascript"> photos.jcorpwidth = 400; ["200", "80"]; // ขนาดภาพ "ความกว้าง" อาร์เรย์, กำหนดความกว้างของภาพที่แตกต่างจากขนาดใหญ่ถึงเล็กตามจำนวนรูปภาพ // photos.heights = ["200", "80"]; // ขนาดภาพ "สูง" photos.upload_filetextid = "uptext"; // อัปโหลดส่วนประกอบปุ่มข้อความข้อความ photos.upload_alertid = "upalert"; // คำใบ้ข้อความข้อความ photos.upload_alertclassName = "ข้อผิดพลาด"; "OriginalImg"; // รูปภาพ ID ภาพพื้นฐานรูปภาพ UPLOAD_RELOADID = "UP_RELOAD"; // reUpload ส่วนประกอบ ID รูปภาพ upload_picValueId = "picval"; // อัปโหลดรูปภาพ ID ภาพด้านล่าง upload_picfileid = "fileid"; /// photos.upload_picimgheight = "imgheight"; // ภาพฐานที่อัพโหลดสูงภาพถ่าย upload_picimgwidth = "imgwidth"; // ภาพฐานที่อัพโหลดภาพกว้าง upload_temporiginvalueid = "temporigicpic"; // "origpicval"; // รหัสเนื้อหาภาพต้นฉบับเมื่อหน้าอื่น ๆ ป้อนรูปภาพ UPLOAD_ROLLBARID = "PIC2"; // รูปถ่าย SCROLLBAR ID ที่กำหนดเอง (เริ่มต้นจาก 1) ตัวอย่าง: id = "photos_preview1" ... photos.parentfileimgid = "imgurl"; // parent picture control id photos.parentFilevalueId = "imgid"; // ที่อยู่รูปภาพของผู้ปกครอง photos.uptext_format = "ขออภัยเฉพาะรูปแบบ jpg, png และ bmp เท่านั้นที่ได้รับการสนับสนุนในช่วงเวลา"; // รูปแบบรูปภาพการตรวจสอบรูปแบบข้อความ/*photos.uptext_size = "ขออภัยขนาดภาพไม่เกิน 2m"; "/enterprise/filescheck"; // address photos.url_upload = "/enterprise/uploadphoto? up = ต้นฉบับ"; // อัปโหลดภาพการโหลดรูปภาพรูปภาพพื้นฐานรูปภาพ. url_reload = "";รหัสพื้นหลัง Java
เครื่องมืออัปโหลดรูปภาพ
แพ็คเกจ com.shengya.service.utils; นำเข้า com.shengya.service.imgcontants; นำเข้า org.apache.commons.fileupload.disk.diskfileitem; นำเข้า org.springframework.web.multipart.multipartfile; org.springframework.web.multipart.Commons.CommonsMultipartFile นำเข้า sun.misc.base64encoder; นำเข้า Javax.crypto.mac; นำเข้า Javax.crypto.secretkey; javax.imageio.imagereadparam; นำเข้า java.awt.*; นำเข้า java.awt.geom.affinetransform; นำเข้า java.awt.image.affinetransformop; นำเข้า java.awt.image.bufferedimage; java.net.urlencoder; นำเข้า java.util.arraylist;/** * @author Darcy * สร้างโดยผู้ดูแลระบบเมื่อปี 2016/6/17 */คลาสสาธารณะ Uploadutils {// Public Final String String สุดท้าย radiourl = "http://211.102.216.237:8011/fileserver/webapi/attachments/bio?"; Public Static String สุดท้าย radiourl = "http://192.168.1.15:8088/fileserver/webapi/attachments/bio?"; public final String url = imgcontants.img_upload + "/file/uploadimage?"; สตริงคงสุดท้ายสุดท้ายสาธารณะ fileUrl = imgContants.img_upload + "/ไฟล์/uploadfile?"; สตริงคงสุดท้ายสาธารณะ vediourl = imgContants.file_upload + "/ไฟล์/uploadfile?"; Public Final String cropurl = imgcontants.img_upload + "/ไฟล์/cropimage?"; Private Static Byte [] ReadAsByTearr (InputStream คือ) {ByTeArrayOutputStream BAOS = ใหม่ byteArrayOutputStream (); ลอง {byte [] bytes = byte ใหม่ [1024]; ความยาว int = 0; ในขณะที่ ((length = is.read (bytes))! = -1) {baos.write (ไบต์, 0, ความยาว); } return baos.tobytearray (); } catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {baos.close (); } catch (ioexception e) {e.printstacktrace (); }} return null; } / ** * อัปโหลดรูปภาพ * * ไฟล์ @param * / สตริงคงที่สาธารณะ submentImage (ไฟล์ไฟล์) พ่น unsupportencodexception {string filename = urlencoder.encode (file.getName (), "UTF-8"); Long Filesize = file.length (); บูลีน isresuffix = true; ความสูง int = 0; ความกว้าง int = 500; String Mark = "T"; โหมดสตริง = "W"; String url = sign (url) + "& channelno = muying_android" + "& filename =" + filename + "& filesize =" + filesize + "& isresuffix =" + isresuffix + "& ความสูง =" ความสูง + "& width =" + ความกว้าง + "& mark =" + " System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {bin = ใหม่ bufferedInputStream (ใหม่ FileInputStream (ไฟล์)); conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); // conn.setFixedLengthStreamingMode (file.length ()); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", file.length () + ""); // conn.setRequestProperty ("ช่วง", "bytes ="+""); // ตั้งค่าการเข้ารหัสอักขระของ httpurlconnection conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); ไบต์ [] buf = ไบต์ใหม่ [1024]; int len = 0; int lencount = 0; ในขณะที่ ((len = bin.read (buf))! = -1) {outstream.write (buf, 0, len); // ok lencount = lencount + len; } Outstream.flush (); int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; } / ** * อัปโหลดรูปภาพ * * ไฟล์ @param * / สตริงคงที่สาธารณะ submentWebimage (ไฟล์ multipartFile) พ่น unsupportencodexception {สตริง filename = urlencoder.encode (file.getoriginalfilename (), "UTF-8"); long filesize = file.getSize (); บูลีน isresuffix = true; ความสูง int = 0; ความกว้าง int = 500; String Mark = "T"; โหมดสตริง = "W"; String url = sign (url) + "& channelno = muying_android" + "& filename =" + filename + "& filesize =" + filesize + "& isresuffix =" + isresuffix + "& ความสูง =" ความสูง + "& width =" + ความกว้าง + "& mark =" + " System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {bin = ใหม่ bufferedInputStream (file.getInputStream ()); conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); // conn.setFixedLengthStreamingMode (file.length ()); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", file.getSize () + ""); // conn.setRequestProperty ("ช่วง", "bytes ="+""); // ตั้งค่าการเข้ารหัสอักขระของ httpurlconnection conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); ไบต์ [] buf = ไบต์ใหม่ [1024]; int len = 0; int lencount = 0; ในขณะที่ ((len = bin.read (buf))! = -1) {outstream.write (buf, 0, len); // ok lencount = lencount + len; } Outstream.flush (); int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; } / ** * อัปโหลดวิดีโอ * * ไฟล์ @param * / สตริงคงที่สาธารณะ submentRadio (ไฟล์ multipartFile) พ่นข้อยกเว้น {string url = signRadio (radiourl); System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {bin = ใหม่ bufferedInputStream (file.getInputStream ()); conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", file.getSize () + ""); conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); ไบต์ [] buf = ไบต์ใหม่ [1024]; int len = 0; int lencount = 0; ในขณะที่ ((len = bin.read (buf))! = -1) {outstream.write (buf, 0, len); // ok lencount = lencount + len; } Outstream.flush (); int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; } /** * อัปโหลด Snapshot * * @param filename * @param เนื้อหา * @return * @throws unsupportencodingexception * /สตริงคงที่สาธารณะ submentfile (สตริงชื่อไฟล์สตริง filename = urlencoder.encode (ชื่อไฟล์, "UTF-8"); ไบต์ [] contentBytes = content.getBytes (); Long Filesize = ContentBytes.Length; บูลีน isresuffix = true; ความสูง int = 0; ความกว้าง int = 500; String Mark = "T"; โหมดสตริง = "W"; string url = sign (fileUrl) + "& channelno = muying_android" + "& filename =" + filename + "& filesize =" + filesize + "& isresuffix =" + isresuffix + "& ความสูง =" ความสูง + "& width =" + ความกว้าง System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); // conn.setFixedLengthStreamingMode (file.length ()); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", content.length () + ""); // conn.setRequestProperty ("ช่วง", "bytes ="+""); // ตั้งค่าการเข้ารหัสอักขระของ httpurlconnection conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); Outstream.write (ContentBytes, 0, ContentBytes.length); // ok outstream.flush (); int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; } / *** อัปโหลดไฟล์แนบ** @return json string* @throws unsupportencodingexception* / สตริงคงที่สาธารณะ submentfile (ไฟล์ multipartfile) โยน unsupportencodexception {สตริง filename = urlencoder.encode (file.getoriginalfilename () long filesize = file.getSize (); บูลีน isresuffix = true; ความสูง int = 0; ความกว้าง int = 500; String Mark = "T"; โหมดสตริง = "W"; string url = sign (fileUrl) + "& channelno = muying_android" + "& filename =" + filename + "& filesize =" + filesize + "& isresuffix =" + isresuffix + "& ความสูง =" ความสูง + "& width =" + ความกว้าง System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {bin = ใหม่ bufferedInputStream (file.getInputStream ()); conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); // conn.setFixedLengthStreamingMode (file.length ()); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", file.getSize () + ""); // conn.setRequestProperty ("ช่วง", "bytes ="+""); // ตั้งค่าการเข้ารหัสอักขระของ httpurlconnection conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); ไบต์ [] buf = ไบต์ใหม่ [1024]; int len = 0; int lencount = 0; ในขณะที่ ((len = bin.read (buf))! = -1) {outstream.write (buf, 0, len); // ok lencount = lencount + len; } int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; } เครื่องหมายสตริงคงที่ส่วนตัว (String baseUrl) พ่น unsupportencodeNingException {currency long = system.currentTimeMillis (); การประทับเวลายาว = สกุลเงิน / 1000; String data = "ActionName = UploadImage" + "& SecretKey = 4B19F08DBF0ADB82CE9CC7C207EC1DC9" + "& timestamp =" + timestamp; String Signature = HMAC_SHA1 ("4B19F08DBF0ADB82CE9CC7C207EC1DC9", urlenCoder.ENCODE (ข้อมูล, "UTF-8")); Signature = Signature.replace ("%3D", "%3D"); Signature = Signature.replace ("%3a", "%3a"); Signature = Signature.replace ("%2F", "%2F"); ส่งคืน baseUrl + data + "& signature =" + ลายเซ็น; } ส่วนตัวสตริงคงที่ signRadio (url string) พ่นข้อยกเว้น {long currencybttime = system.currentTimeMillis (); String data = "SecretKey = F1B79C865C424BE46183A2F0A49A0F15" + "& timestamp =" + สกุลเงิน; System.out.println ("url:" + url + data); System.out.println ("url =" + urlencoder.encode (url + data, "UTF-8")); String Signature = HMACSHA1UTIL.GETSIGNATURE (urlencoder.ENCODE (URL + DATA, "UTF-8"), "F1B79C865C424BE46183A2F0A49A0F15"); ส่งคืน URL + data + "& signature =" + ลายเซ็น; } สตริงคงที่ส่วนตัว HMAC_SHA1 (คีย์สตริง, ข้อมูลสตริง) {string restring = ""; ลอง {datas = datas.replace ("%3a", "%3a"); datas = datas.replace ("%2f", "%2f"); datas = dataS.replace ("%3D", "%3D"); ไบต์ [] data = key.getBytes ("UTF-8"); // สร้างคีย์ตามอาร์เรย์ไบต์ที่กำหนดและพารามิเตอร์ที่สองระบุชื่อของอัลกอริทึมคีย์ SecretKey SecretKey = ใหม่ SecretKeySpec (ข้อมูล "HMACSHA1"); // สร้างวัตถุ MAC ด้วยอัลกอริทึม MAC ที่ระบุ Mac Mac = Mac.getInstance ("HMACSHA1"); // เริ่มต้นวัตถุ MAC ด้วยคีย์ mac.init (SecretKey); ไบต์ [] text = dataS.getBytes ("UTF-8"); // การดำเนินการ MAC ที่สมบูรณ์ไบต์ [] text1 = mac.dofinal (ข้อความ); REDRING = ใหม่ BASE64ENCODER (). ENCODEBUFFER (text1); Restring = urlencoder.encode (Restring, "UTF-8"); REDRING = RESTRING.REPALE ("%0A", ""); Restring = Restring.Replace ("%0d", ""); } catch (exception e) {e.printstacktrace (); } return restring; }/** * อัปโหลดรูปภาพที่ถูกครอบตัด * ZXIAO 2016/09/01 * * @param fileId */สตริงคงที่สาธารณะที่ส่งไปที่ WebImage (ไฟล์ MultipartFile, String fileid, String X, String W, String H) String [] cropWidths = สตริงใหม่ [] {"50"}; // ความกว้างของพืช // รายการ <String> รายการ = รายการใหม่ <String> (); // list <string> list = new ArrayList <String> (5); arrayList <String> listCropWidths = new ArrayList <String> (); listcropwidths.add ("400"); // string [] cropheights = สตริงใหม่ [] {"50"}; // ความสูงของพืช ArrayList <String> listcropheights = new ArrayList <String> (); listcropheights.add ("300"); int cropx = integer.parseint (x); // crop x พิกัด int cropy = integer.parseint (y); // crop y Coordinates // string [] cropfixs = สตริงใหม่ [] {"l"}; // listcropfixs.add ("l"); สตริง sourcesuffix = "jpg"; // ฐานแผนที่คำต่อท้ายสตริง sourcefix = ""; // โลโก้แผนที่ฐาน/* บูลีน isresuffix = true; ความสูง int = 1,000; ความกว้าง int = 750; String Mark = "T"; โหมดสตริง = "w";*/ string url = sign (cropurl) + "fileid =" + fileid + "& cropwidths =" + listcropwidths + "& cropheights =" + listcropheights + "& cropx =" + cropx + "& cropy =" sourcesuffix + "& sourcefix =" + sourcefix; System.out.println ("url:" + url); httpurlConnection conn = null; OutputStream Outstream = NULL; bufferedInputStream bin = null; ลอง {bin = ใหม่ bufferedInputStream (file.getInputStream ()); conn = (httpurlconnection) url ใหม่ (url) .openconnection (); conn.setConnectTimeout (15000); conn.setRequestMethod ("โพสต์"); conn.setDooutput (จริง); // conn.setFixedLengthStreamingMode (file.length ()); conn.setRequestProperty ("ประเภทเนื้อหา", "แอปพลิเคชัน/x-www-form-urlencoded"); conn.setRequestProperty ("ความยาวเนื้อหา", file.getSize () + ""); // conn.setRequestProperty ("ช่วง", "bytes ="+""); // ตั้งค่าการเข้ารหัสอักขระของ httpurlconnection conn.setRequestProperty ("Accept-Charset", "UTF-8"); Outstream = conn.getOutputStream (); ไบต์ [] buf = ไบต์ใหม่ [1024]; int len = 0; int lencount = 0; ในขณะที่ ((len = bin.read (buf))! = -1) {outstream.write (buf, 0, len); // ok lencount = lencount + len; } int responsecode = conn.getResponsecode (); if (responsecode == 200) {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; } else {inputStream instream = conn.getInputStream (); ไบต์ [] data1 = readasbytearr (stream); String JSON = สตริงใหม่ (data1); Instream.close (); กลับ JSON; }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (Outstream! = null) {Outstream.close (); }} catch (ioexception e) {e.printstacktrace (); }} return null; }/** ภาพต้องได้รับการบันทึกในเครื่องหลังจากอัปโหลดและการครอบตัด * Zxiao 2016/09/01 * @param, * @param x * @param y * @param w * @param h * @return * @throws ioexception */สตริงคงที่ {String Folder = System.getProperty ("java.io.tmpdir"); ไฟล์ tempdir = ไฟล์ใหม่ (โฟลเดอร์); // ถ้าโฟลเดอร์ไม่มีอยู่ให้สร้างถ้า (! tempdir .Exists () &&! tempdir .isdir ()) {// system.out.println ("// ไม่มีอยู่"); Tempdir .Mkdir (); } // สตริงไฟล์ชั่วคราวบีบอัด srcPath = โฟลเดอร์+"scaletemp.jpg"; // รับคำต่อท้ายของไฟล์/*ไฟล์ uploadfile = ไฟล์ใหม่ (srcpath); inputStream inputStream = file.getInputStream (); FileInputStream FS = (FileInputStream) InputStream; String srcimg = urlencoder.encode (file.getoriginalfilename (), "UTF-8"); String Suffix = srcimg.substring (srcimg.lastindexof (".")+1);*// // crop ภาพ cutimgutils o = cutimgutils ใหม่ (x, y, w, h); O.SetSrcPath (SRCPath); O.SetSubPath (โฟลเดอร์+"uptemp.jpg"); ไฟล์ tempfile = o.cut ("jpg"); String s = submentImage (tempfile); กลับ s; }/** * รับความกว้างของภาพที่อัปโหลด * ZXIAO 2016/09/09 * ไฟล์ @param * @return */สาธารณะคงที่ int getimgwidth (ไฟล์ multipartfile) โยน ioexception {inputstream คือ = null; bufferedImage src = null; int ret = -1; ลอง {is = file.getInputStream (); src = javax.imageio.imageio.read (IS); ret = src.getWidth (null); // รับความกว้างของภาพต้นทางคือ. close (); } catch (exception e) {e.printstacktrace (); } return ret; }/** * รับภาพที่อัปโหลดสูง * ZXIAO 2016/09/09 * ไฟล์ @param * @return */สาธารณะคงที่ int getimgheight (ไฟล์ multipartFile) {inputStream คือ = null; bufferedImage src = null; int ret = -1; ลอง {is = file.getInputStream (); src = javax.imageio.imageio.read (IS); ret = src.getheight (null); // รับความสูงของภาพต้นทางคือ. close (); } catch (exception e) {e.printstacktrace (); } return ret; } /*** ภาพมาตราส่วน (การปรับขนาดตามความสูงและความกว้าง)* @param ไฟล์ไฟล์ไฟล์* @param ผลลัพธ์ที่อยู่ภาพสเกล* @param ความสูงระดับความสูง* @param ความกว้างความกว้างของสเกล* @param bb จำเป็นต้องเติมเมื่อเครื่องชั่งไม่ถูกต้อง: จริงคือการเติม; เท็จไม่ได้เติม */ สเกลโมฆะคงที่สาธารณะ (ไฟล์ multipartFile, สตริงผลลัพธ์, ความสูง int, ความกว้าง int, บูลีน bb) {ลอง {อัตราส่วนคู่ = 0.0; // scaling/*commonsmultipartfile cf = (commonsmultipartfile) ไฟล์; diskfileitem fi = (diskfileitem) cf.getFileItem (); ไฟล์ f = fi.getStorElocation ();*/// ไฟล์ f = ไฟล์ใหม่ (srcimageFile); inputStream inputStream = file.getInputStream (); bufferedImage bi = imageio.read (inputstream); Image Item = bi.getScaledInstance (ความกว้าง, ความสูง, bi.scale_smooth); // คำนวณสเกลถ้า ((bi.getheight ()> ความสูง) || (bi.getWidth ()> ความกว้าง)) {ถ้า (bi.getheight ()> bi.getWidth ()) {อัตราส่วน = (ความสูงใหม่) } else {ratio = (จำนวนเต็มใหม่ (ความกว้าง)). doubleValue () / bi.getWidth (); } affineTransformop op = new affineTransformop (affineTransform .getScaleInstance (อัตราส่วน, อัตราส่วน), null); item = op.filter (bi, null); } if (bb) {// เติม bufferedImage image = new bufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); graphics2d g = image.createGraphics (); G.SetColor (color.white); G.FillRect (0, 0, ความกว้าง, ความสูง); if (width == item.getWidth (null)) g.DrawImage (itemp, 0, (ความสูง - item.getheight (null)) / 2, item.getWidth (null), item.getheight (null), color.white, null); อื่น G.DrawImage (รายการ, (ความกว้าง - item.getWidth (null)) / 2, 0, item.getWidth (null), item.getheight (null), color.white, null); G.Dispose (); item = image; } imageio.write ((bufferedImage) รายการ, "jpg", ไฟล์ใหม่ (ผลลัพธ์)); } catch (ioexception e) {e.printstacktrace (); }} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ลอง {/* ไฟล์ไฟล์ = ไฟล์ใหม่ ("e: // bole // doc // เอกสารอินเตอร์เฟสบริการ // bole service interface คำอธิบาย v1.0.docx"); System.out.println ("file =" + ไฟล์); String JSON = submitFile (ไฟล์); System.out.println ("json =" + json);*/} catch (Exception e) {e.printstacktrace (); - เครื่องมือการปลูกถ่ายภาพ
แพ็คเกจ com.shengya.service.utils; นำเข้า Javax.imageio.imageio; นำเข้า Javax.imageio.imagereadparam; นำเข้า Javax.imageio.imagereader; นำเข้า Javax.imageio.stream.imageinputstream; นำเข้า Java.awt. java.io.fileinputstream; นำเข้า java.io.ioException; นำเข้า java.util.iterator;/*** สร้างโดยผู้ดูแลระบบเมื่อปี 2016/9/1 */คลาสสาธารณะ cutimgutils {// === ชื่อเส้นทางภาพต้นฉบับเช่น: C: /1.jpg สตริงส่วนตัว srcpath; // === ชื่อเส้นทางการจัดเก็บภาพคลิป ตัวอย่างเช่น: C: /2.jpg Subpath String ส่วนตัว; โมฆะสาธารณะ setsrcpath (String srcpath) {this.srcpath = srcpath; } โมฆะสาธารณะ SetSubPath (Subpath String) {this.subpath = subpath; } // === Slice Point X พิกัดส่วนตัว int x; ส่วนตัว int y; // === ความกว้างของจุด slift ความกว้าง int ส่วนตัว; ความสูง int ส่วนตัว; public cutimgutils () {} public cutimgutils (int x, int y, ความกว้าง int, ความสูง int) {ถ้า (x <0) {x = 0; } else {this.x = x; } ถ้า (y <0) {y = 0; } else {this.y = y; } this.width = ความกว้าง; this.height = ความสูง; } /** * * ครอบตัดภาพและบันทึกภาพใหม่หลังจากการครอบตัด */ ไฟล์สาธารณะตัด () พ่น IOException {fileInputStream คือ = null; ImageInputStream IIS = NULL; ลอง {// อ่านไฟล์ภาพคือ = ใหม่ FileInputStream (SRCPath); /** * * ส่งคืนตัววนซ้ำที่มี imageReaders ที่ลงทะเบียนทั้งหมดซึ่งอ้างว่าสามารถถอดรหัสรูปแบบที่ระบุได้ พารามิเตอร์: FormatName - มีชื่อรูปแบบที่ไม่เป็นทางการ * * (เช่น "jpeg" หรือ "tiff") ฯลฯ */ iterator <AmageReader> it = imageio .getImageEdersByFormatName ("jpg"); imageReader reader = it.next (); // รับ image stream iis = imageio.createImageInputStream (IS); /** * * <p> * iis: อ่านแหล่งที่มา จริง: ค้นหาไปข้างหน้าเท่านั้น * </p> * ทำเครื่องหมายว่า 'ค้นหาไปข้างหน้าเท่านั้น' * * การตั้งค่านี้หมายความว่ารูปภาพที่รวมอยู่ในแหล่งอินพุตจะถูกอ่านตามลำดับเท่านั้นซึ่งอาจอนุญาตให้ผู้อ่าน * * หลีกเลี่ยงการแคชส่วนอินพุตเหล่านั้นที่มีข้อมูลที่เกี่ยวข้องกับภาพที่อ่านก่อนหน้านี้ */ reader.setInput (iis, true); /** * * <p> * คลาสที่อธิบายวิธีการถอดรหัสสตรีม * <p> * ใช้เพื่อระบุวิธีการแปลงภาพหรือชุดภาพจากสตรีมในบริบทของกรอบภาพ Java I/O * * เมื่อป้อน ปลั๊กอินสำหรับรูปแบบภาพเฉพาะ * * * อินสแตนซ์ของ imageReadParam จะถูกส่งคืนจากวิธีการใช้งานของ imageReader ของ GetDefaultreadParam */ imageReadParam param = reader.getDefaulTreadParam (); /** * * พื้นที่การเพาะปลูกภาพ สี่เหลี่ยมผืนผ้าระบุพื้นที่ในพื้นที่พิกัดซึ่งสามารถกำหนดได้โดยพิกัด (x, y), ความกว้างและความสูงของจุดสุดยอดด้านบนซ้ายของวัตถุสี่เหลี่ยมผืนผ้า ** */ สี่เหลี่ยมผืนผ้าสี่เหลี่ยม = สี่เหลี่ยมใหม่ (x, y, ความกว้าง, ความสูง); // ระบุ bufferedImage เพื่อใช้เป็นเป้าหมายสำหรับการถอดรหัสข้อมูลพิกเซล param.setSourceRegion (rect); /** * ใช้ imageReadParam ที่ให้ไว้เพื่ออ่านวัตถุที่ระบุโดยการจัดทำดัชนี ImageIndex และส่งคืน * * เป็น bufferedImage ที่สมบูรณ์ */ bufferedImage bi = reader.read (0, param); // บันทึกภาพใหม่ Imageio.write (bi, "jpg", ไฟล์ใหม่ (subpath)); ไฟล์ไฟล์ = ไฟล์ใหม่ ("subpath"); ส่งคืนไฟล์; } ในที่สุด {ถ้า (คือ! = null) คือ is.close (); ถ้า (iis! = null) iis.close (); }} การตัดไฟล์สาธารณะ (สตริงคำต่อท้าย) พ่น IOException {fileInputStream คือ = null; ImageInputStream IIS = NULL; ลอง {// อ่านไฟล์ภาพคือ = ใหม่ FileInputStream (srcPath); // is = inputStream; /** * ส่งคืนตัววนซ้ำที่มี imagereaders ที่ลงทะเบียนทั้งหมดในปัจจุบัน * * อ้างว่าสามารถถอดรหัสรูปแบบที่ระบุได้ พารามิเตอร์: FormatName - มีชื่อรูปแบบที่ไม่เป็นทางการ * * (เช่น "jpeg" หรือ "tiff") ฯลฯ * ตัววนซ้ำ <ageMeereader> it = imageio.getimageEdersByFormatName ("jpg"); */ string suffixName = null; if (suffix.equals ("gif")) {suffixName = "gif"; } else if (suffix.equals ("png")) {suffixName = "png"; } else {suffixName = "jpg"; } iterator <AmageReader> it = imageio .getImageEdersByFormatName (SuffixName); imageReader reader = it.next (); // รับ image stream iis = imageio.createImageInputStream (IS); /** * * <p> * iis: อ่านแหล่งที่มา จริง: ค้นหาไปข้างหน้าเท่านั้น * </p> * ทำเครื่องหมายว่า 'ค้นหาไปข้างหน้าเท่านั้น' * * การตั้งค่านี้หมายความว่ารูปภาพที่รวมอยู่ในแหล่งอินพุตจะถูกอ่านตามลำดับเท่านั้นซึ่งอาจอนุญาตให้ผู้อ่าน * * หลีกเลี่ยงการแคชส่วนอินพุตเหล่านั้นที่มีข้อมูลที่เกี่ยวข้องกับภาพที่อ่านก่อนหน้านี้ */ reader.setInput (iis, true); /** * * <p> * คลาสที่อธิบายวิธีการถอดรหัสสตรีม * <p> * ใช้เพื่อระบุวิธีการแปลงภาพหรือชุดภาพจากสตรีมในบริบทของกรอบภาพ Java I/O * * เมื่อป้อน Plugin for a specific image format* * * An instance of ImageReadParam will be returned from its ImageReader implementation's getDefaultReadParam method. */ ImageReadParam param = reader.getDefaultReadParam(); /** * * Image crop area. Rectangle specifies an area in the coordinate space, which can be defined by the coordinate (x, y), width, and height of the upper left vertex of the Rectangle object **. */ Rectangle rect = new Rectangle(x, y, width, height); // Provide a BufferedImage to use it as the target for decoding pixel data. param.setSourceRegion(rect); /** * Use the provided ImageReadParam to read the object specified by indexing imageIndex and return * * as a complete BufferedImage. */ BufferedImage bi = reader.read(0, param); // Save new image ImageIO.write(bi, "jpg", new File(subpath)); File file = new File(subpath); return file; } finally { if (is != null) is.close(); if (iis != null) iis.close(); -遇到的问题总结:
-裁剪图片发生偏移现象<br />解决办法:使用压缩后的图片等比例收集裁切坐标信息
-MultipartFile 和File之间相互转化
第一种方法:
MultipartFile file = xxx; CommonsMultipartFile cf= (CommonsMultipartFile)file; DiskFileItem fi = (DiskFileItem)cf.getFileItem(); File f = fi.getStoreLocation();
会在项目的根目录的临时文件夹下生成一个文件;
第二种方法:
transferTo(File dest);
会在项目中生成一个新文件;
第三种方法:
File f = (File) xxx 强转即可。前提是要配置multipartResolver,要不然会报类型转换失败的异常。
<bean id="multipartResolver"> <property name="maxUploadSize" value="104857600"/> <property name="maxInMemorySize" value="4096"/> </bean>
没试过;
第四种方法:
Workbook wb = Workbook.getWorkbook(xxx .getInputStream());
转换为输入流,直接读取;
第五种方法:
byte[] buffer = myfile.getBytes();
先转换为字节数组,没试过;
第六种方法:
直接转成输入流
MultipartFile file=xxx; is = file.getInputStream(); src = javax.imageio.ImageIO.read(is); ret = src.getWidth(null); // 得到源图宽
参考资料:
jcop api:http://code.ciaoca.com/jquery/jcrop/.
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น