บทความนี้อธิบายวิธีการอัปโหลดรูปภาพสำหรับการตัดโดย Java แบ่งปันสำหรับการอ้างอิงของคุณ การวิเคราะห์เฉพาะมีดังนี้:
เหตุใดฉันจึงต้องการตัดรูปภาพที่อัปโหลดไว้ในโครงการของฉันคือโลโก้ของแผนก ส่วนที่เหลือของชิ้นส่วนเนื่องจากการเปลี่ยนแปลงด้านซ้ายและกลางอาจมีรูปแบบหรือตัวละครดังนั้นจึงไม่สามารถยืดได้ ดังนั้นฉันจึงใช้ ImageReader of Javax เพื่อใช้งาน สุดท้ายบันทึกฐานข้อมูลเป็นประเภทหยด
ขั้นแรกให้เขียน Enctype = "multipart/form-data" ในแบบฟอร์มแบบฟอร์ม
คัดลอกรหัสรหัสดังต่อไปนี้: <รูปแบบวิธี = "โพสต์" id = "mainform" action = "$ {ctx} /admin/department!
ต่อไปนี้เป็นรหัสต่อไปนี้สำหรับการอัปโหลดรหัสการคัดลอกส่วนประกอบในแบบฟอร์ม: <tr>
<td>
<strong> โลโก้อัปโหลด: </strong>
<input type = "hidden" name = "suffix" id = "suffix" value = "$ {hepect.departmentlogo.suffix}"/>
</td>
<td>
<input type = "file" name = "logofile" id = "logofile" onChange = "checkFile ();"/>
</td>
</tr>
<tr>
<td> </td>
<td>
<span> (รูปแบบภาพ: jpg, jpeg, bmp, gif, png; </span> <br/>
<span> ขนาดที่แนะนำ: 1120 × 80 พิกเซล) </span>
</td>
</tr>
วิธีการตรวจสอบภาพ checkfile () ใช้เพื่อตรวจสอบว่าภาพสิ้นสุดในรูปแบบที่ระบุหรือไม่และไม่มีการเลือกรหัสคัดลอกรหัสดังนี้: ฟังก์ชั่น checkfile () {
var value = $ ("#logofile"). val ();
ถ้า (! ค่า) {
การแจ้งเตือน ("โปรดเลือกรูปภาพที่คุณต้องการอัปโหลด!");
กลับเท็จ;
}อื่น{
if (value.lastindexof (". jpg")! = -1) {
$ ("#ต่อท้าย"). val ("jpg");
กลับมาจริง;
} อื่นถ้า (value.lastindexof (". jpeg")! = -1) {
$ ("#ต่อท้าย"). val ("jpeg");
กลับมาจริง;
} อื่นถ้า (value.lastindexof (". gif")! = -1) {
$ ("#ต่อท้าย"). val ("gif");
กลับมาจริง;
} อื่นถ้า (value.lastindexof (". bmp")! = -1) {
$ ("#ต่อท้าย"). Val ("BMP");
กลับมาจริง;
} อื่นถ้า (value.lastindexof (". png")! = -1) {
$ ("#ต่อท้าย"). val ("png");
กลับมาจริง;
}อื่น{
การแจ้งเตือน ("ขออภัยรูปแบบไฟล์ที่คุณอัปโหลดไม่ถูกต้องโปรดเลือกไฟล์ภาพในรูปแบบที่ระบุเพื่ออัปโหลด");
กลับเท็จ;
-
-
-
ต่อไปนี้คือการดำเนินการบันทึกพื้นหลัง
คัดลอกรหัสดังนี้: สตริงสาธารณะบันทึก () พ่นข้อยกเว้น {
httpservletRequest request = servletactionContext.getRequest ();
String DepartId = request.getParameter ("id");
String ishedName = request.getParameter ("ชื่อ");
String companyId = request.getParameter ("companyId");
// คำต่อท้ายรูปภาพ
สตริงคำต่อท้าย = request.getParameter ("คำต่อท้าย");
รายการ <เมนู> listmenu = arraylist ใหม่ <men> ();
บริษัท บริษัท = บริษัท ใหม่ ();
company.setId (long.valueof (companyid));
if (this.logofile! = null && ischedName! = null && companyId! = null && ต่อท้าย! = null) {
// รับ Imagereader สำหรับการแยกวิเคราะห์ภาพ
Iterator <AmageReader> ImageReaders = imageio.getimageEdersByFormatName (คำต่อท้าย);
imageReader imageReader = imageReaders.next ();
// ส่งภาพในรูปแบบของสตรีมไบต์
InputStream Logostream = ใหม่ bufferedInputStream (ใหม่ FileInputStream (this.logofile));
// แปลงเป็นสตรีมอินพุตรูปภาพ
imageInputStream imageInputStream = imageio.createImageInputStream (โลโก้);
ImageReader.SetInput (ImageInputStream, TRUE);
int imageWidth = imageReader.getWidth (0);
// ความสูงคงที่ 80
int imageHeight = 80;
// ตั้งค่าซ้ายตรงกลางขวาและความกว้างที่เหลืออยู่
int leftwidth = imagewidth / 2;
int middlewidth = (imageWidth - leftwidth) / 3;
Int Rardwidth = 5;
int retainWidth = imageWidth - leftWidth -middleWidth - 5;
ImageReadParam readparam = imageReader.getDefaultreadParam ();
bufferedImage bimage = null;
// ครอบตัดครึ่งซ้าย
// รับสี่เหลี่ยมผืนผ้าตามความกว้างและความสูง
สี่เหลี่ยมผืนผ้า leftimageCtangle = สี่เหลี่ยมใหม่ (0, 0, ซ้าย, imageHeight);
readparam.setsourceregion (leftimageectangle);
bimage = imageReader.read (0, readparam);
สตรีมเอาต์พุตอาร์เรย์อาร์เรย์ไบต์
ByTeArrayOutputStream leftByTeArrayOutputStream = ใหม่ byteArrayOutputStream ();
imageio.write (bimage, ต่อท้าย, leftbytearrayoutputstream);
// รับอาร์เรย์ไบต์
Byte [] leftimagedata = leftbyTeArrayOutputStream.tobyteArray ();
leftbyTeArrayOutputStream.close ();
// ไฮเบอร์เนตสร้างประเภทหยด
blob leftblob = hibernate.createBlob (leftimagedata, this.departmentmanager.getSession ());
// ครอบตัดส่วนกลาง
สี่เหลี่ยมผืนผ้า MiddleImagerectangle = สี่เหลี่ยมใหม่ (ซ้าย width, 0, middlewidth, imageHeight);
readparam.setsourceregion (Middenimageectangle);
bimage = imageReader.read (0, readparam);
ByTeArrayOutputStream MidDlearrayOutputStream = ใหม่ ByTeArrayOutputStream ();
Imageio.write (bimage, ต่อท้าย, middlearrayoutputstream);
Byte [] MiddleImagedata = MiddlearrayOutputStream.tobyteArray ();
MiddlearrayOutputStream.close ();
Blob Middleblob = Hibernate.CreateBlob (MiddleImagedata, this.departmentManager.getSession ());
// ครอบตัดครึ่งขวา
สี่เหลี่ยมผืนผ้า rightimageCtangle = รูปสี่เหลี่ยมผืนผ้าใหม่ (leftwidth + Middlewidth, 0, Rightwidth, ImageHeight);
readparam.setsourceregion (Rightimagerectangle);
bimage = imageReader.read (0, readparam);
ByTeArrayOutputStream rightArrayOutputStream = ใหม่ byteArrayOutputStream ();
Imageio.write (bimage, ต่อท้าย, rightArrayOutputStream);
Byte [] Rightimagedata = RightarrayOutputStream.tobyteArray ();
rightArrayOutputStream.close ();
BLOB RIGHTBLOB = HIBERNATE.CREATEBLOB (RightImagedata, this.departmentManager.getSession ());
// สำรองส่วน
สี่เหลี่ยมผืนผ้า retainRectangle = สี่เหลี่ยมใหม่ (ซ้าย width + middlewidth + rightwidth, 0, retainwidth, imageHeight);
readparam.setsourceregion (retainRectAngle);
bimage = imageReader.read (0, readparam);
ByTeArrayOutputStream RetainArrayOutputStream = ใหม่ byteArrayOutputStream ();
Imageio.write (bimage, ต่อท้าย, retainarrayoutputStream);
ไบต์ [] retainimagedata = retainArrayoutputStream.tobytearray ();
retainArrayOutputStream.close ();
blob retainblob = hibernate.createBlob (retainimagedata, this.departmentmanager.getSession ());
if (! ectionid.equals ("") && ectionid! = null) {
แผนก d = this.departmentmanager.findbyid (long.valueof (vectionid));
if (this.CheckedAuthids! = null) {
สำหรับ (int i = 0; i <checkedauthids.size (); i ++) {
เมนูเมนู = เมนูใหม่ ();
menu.setid (checkedauthids.get (i));
listmenu.add (เมนู);
-
D.SetMenus (ListMenu);
-
d.getDepartmentlogo (). setleftpartimage (ซ้าย blob);
D.GetDepartmentLogo (). SetMiddlePartImage (MiddleBlob);
d.getDepartmentlogo (). setrightPartImage (RightBlob);
d.getDepartmentlogo (). setRetainPartImage (retainblob);
d.getDepartmentlogo (). setCreateTime (วันที่ใหม่ ());
d.getDepartmentlogo (). setsuffix (คำต่อท้าย);
d.setName (ischedName);
d.setParentId (0L);
d.setNodeType (1);
d.setGrade (1);
D.SetCompany (บริษัท );
this.departmentManager.save (D);
}อื่น{
จำนวนเต็ม parentNodeType = 0;
แผนก DD = แผนกใหม่ ();
if (this.CheckedAuthids! = null && this.checkedauthids.size ()! = 0) {
สำหรับ (int i = 0; i <checkedauthids.size (); i ++) {
เมนูเมนู = เมนูใหม่ ();
menu.setid (checkedauthids.get (i));
listmenu.add (เมนู);
-
dd.setmenus (listmenu);
}อื่น{
dd.setmenus (null);
-
Departmentlogo Departmentlogo = New Departmentlogo ();
Departmentlogo.setCreateTime (วันที่ใหม่ ());
Departmentlogo.SetleftPartImage (ซ้าย blob);
Departmentlogo.SetMiddlePartImage (MiddleBlob);
Departmentlogo.SetrightPartImage (RightBlob);
Departmentlogo.setRetainPartImage (RetainBlob);
Departmentlogo.setsuffix (คำต่อท้าย);
dd.SetDepartmentlogo (Departmentlogo);
dd.getDepartmentlogo (). setDepartment (DD);
dd.setId (this.departmentmanager.findmaxid ()+1);
dd.setName (ischedName);
dd.setParentId (0L);
dd.setNodeType (1);
dd.setGrade (1);
dd.setOrderType (0);
dd.setCompany (บริษัท );
dd.setflag (0);
this.departmentManager.save (DD);
-
-
กลับ "โหลด";
-
ต่อไปนี้จะแสดง
.Welcome-Hide {Width: 210px; White-Space: NowRap; Overflow: Hidden; Text-Overflow: Ellipsis;}
#leftlogo {พื้นหลัง: url ($ {ctx} /admin/department-logo!Showlogo.action?position=Left) ไม่ต้องทำซ้ำความกว้าง: $ {leftwidth} px; ความสูง: 80px;}
#MIDDLELOGO {พื้นหลัง: url ($ {ctx} /admin/department-logo!Showlogo.action?position=middle) ไม่ต้องทำซ้ำ; ความกว้าง: $ {Middlewidth} px; ความสูง: 80px;}}
#rightlogo,#toptablestyle {พื้นหลัง: url ($ {ctx}/admin/department-logo!Showlogo.action?position=RIGHT) ซ้ำ -X; ความสูง: 80px;}
#retainlogo {พื้นหลัง: url ($ {ctx}/admin/department-logo!Showlogo.action?position=retain) ไม่มีการทำซ้ำ; ความกว้าง: $ {retainwidth} px; ความสูง: 80px;}}
</style>
วิธี showlogo ในแผนก logo! showlogo.action คือการโหลดรูปภาพและคัดลอกรหัสรหัสดังนี้: สตริงสาธารณะ showlogo () {
httpservletRequest request = servletactionContext.getRequest ();
logginuser logginuser = (logginuser) ((SecurityContext) request.getSession ()
getAttribute ("Spring_security_context")). getAuthentication (). getPrincipal ();
รายการ <ispartmentlogo> logos = this.logomanager.findall ();
สำหรับ (Departmentlogo Departmentlogo: โลโก้) {
if (logginuser.getUser (). getDepartment (). getId (). เท่ากับ (Departmentlogo.getDepartment (). getId ())) {
String param = request.getParameter ("ตำแหน่ง");
Blob Blob = Null;
if (param! = null) {
พยายาม {
if (param.equals ("ซ้าย")) {
Blob = Departmentlogo.getLeftPartImage ();
imageLogo = blob.getBinaryStream ();
กลับ "showlogo";
} อื่นถ้า (param.equals ("กลาง")) {
Blob = Departmentlogo.getMiddlePartImage ();
imageLogo = blob.getBinaryStream ();
กลับ "showlogo";
} อื่นถ้า (param.equals ("ขวา")) {
Blob = Departmentlogo.getrightPartImage ();
imageLogo = blob.getBinaryStream ();
กลับ "showlogo";
} else if (param.equals ("retain")) {
Blob = Departmentlogo.getRetainPartImage ();
imageLogo = blob.getBinaryStream ();
กลับ "showlogo";
-
} catch (Exception e) {
E.PrintStackTrace ();
-
-
-
-
คืนค่า null;
-
แน่นอนที่อยู่ที่ส่งคืนควรถูกเลือกเป็น type = "stream" ใน struts2
คัดลอกรหัสดังนี้: @Results ({
@Result (name = "showlogo", type = "stream", params = {"contentType", "image/jpeg,"
+ "image/bmp, image/png, image/gif, image/jpeg",
"InputName", "ImageLogo", "Buffersize", "4096"})
-
แล้วคุณจะได้รับความกว้างใน top.jsp ได้อย่างไร?
ฉันได้รับเมื่อโหลดเมนู
รายการ <ispartmentlogo> logos = this.logomanager.findall ();
สำหรับ (Departmentlogo Departmentlogo: โลโก้) {
if (user.getDepartment (). getId (). เท่ากับ (Departmentlogo.getDepartment (). getId ())) {
request.setAttribute ("leftwidth", imageio.read (Departmentlogo
getLeftPartImage (). getBinaryStream ()). getWidth ());
request.setAttribute ("MiddleWidth", Imageio.read (Departmentlogo
getMiddlePartImage (). getBinaryStream ()). getWidth ());
request.setAttribute ("RETAINWIDTH", Imageio.Read (Departmentlogo
getRetainPartImage (). getBinaryStream ()). getWidth ());
หยุดพัก;
-
-
สิ่งนี้จะเสร็จสิ้นฟังก์ชั่นการแสดงผลอัปโหลด
นี่คือภาพทดสอบที่ฉันอัปโหลด
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน