ตัวอย่างในบทความนี้แบ่งปันรหัสสำหรับการอัปโหลดและดาวน์โหลด Java Webservice สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. สร้างโครงการเว็บแบบไดนามิกใหม่ YouMefileserver สร้างแพ็คเกจใหม่ com และสร้างคลาสใหม่ FileProgress
แพ็คเกจ com; นำเข้า java.io.fileinputstream; นำเข้า java.io.fileoutputstream นำเข้า java.io.ioexception; นำเข้า java.sql.date นำเข้า java.text.simpledateformat; นำเข้า java.util.random; นำเข้า sun.misc.base64decoder; และดาวน์โหลดไฟล์*/คลาสสาธารณะ FileProgress {สตริงสาธารณะ Sayshello (ชื่อสตริง) {return "hello,"+name+"/n"+getDir ("2"); }/** บริการอัปโหลดไฟล์*/สตริงสาธารณะ uploadFile (ชื่อไฟล์สตริง, สตริง fileType, ไฟล์สตริง) // byte [] bytes) {fileOutputStream fos = null; ลอง {String filedir = getDir (fileType); Base64Decoder decoder = ใหม่ base64decoder (); ไบต์ [] bytes = decoder.decodeBuffer (ไฟล์); if (filedir == "") {return ""; } Integer RDM = new Random (). NextInt (10,000); String savename = getDatatimestring (จริง) +rdm.toString () +filename.substring (filename.indexof ('.')); FOS = ใหม่ fileOutputStream (filedir+savename); // เขียนข้อมูลในไบต์อาร์เรย์ไบต์ไปยังสตรีมเอาต์พุตไฟล์ fos.write (ไบต์); fos.flush (); ส่งคืน Filedir +Savename; } catch (exception e) {e.printstacktrace (); กลับ ""; } ในที่สุด {ลอง {fos.close (); } catch (ioexception e) {e.printstacktrace (); }}}/** * @param filepath */สตริงส่วนตัว getDir (สตริง fileType) {String path = "f: // youme // {0} //" + getDatastring () + "//"; สวิตช์ (fileType) {case "2": path = path.replace ("{0}", "image"); หยุดพัก; กรณี "3": path = path.replace ("{0}", "Vedio"); หยุดพัก; ค่าเริ่มต้น: return ""; } ลอง {java.io.file file = ใหม่ java.io.file (พา ธ ); if (! file.exists ()) {ถ้า (! file.mkdirs ()) {return ""; }} เส้นทางส่งคืน; } catch (exception ex) {return ""; } ในที่สุด {}}/** บริการดาวน์โหลดไฟล์*/public String downloadfile (String filepath) {// filepath = "f: //youme//vedio//2013-09-03//201309031700143294.amr"; FileInputStream ใน = null; ไบต์ไบต์ [] = null; สตริงไฟล์ = null; ลอง {in = new fileInputStream (filePath); ไบต์ = ไบต์ใหม่ [in.available ()]; // จากสตรีมอินพุตในการอ่านไบต์ของข้อมูลของไบต์ความยาวลงในไบต์อาร์เรย์ไบต์ในการอ่าน (ไบต์); base64Encoder encoder = ใหม่ base64Encoder (); file = encoder.encode (ไบต์); } catch (exception e) {e.printstacktrace (); กลับ ""; } ในที่สุด {ลอง {in.close (); } catch (ioexception e) {e.printstacktrace (); }} // ส่งคืนไบต์; ส่งคืนไฟล์; } /** รับเวลาปัจจุบัน* / สตริงคงที่ส่วนตัว getDatatimestring (บูลีน isfilename) {ลอง {simpledateFormat formatter = null; if (! isfilename) {formatter = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss"); } else {formatter = new SimpledateFormat ("yyyymmddhhmmss"); } วันที่ curdate = วันที่ใหม่ (System.currentTimeMillis ()); // รับ Formatter.Format (Curdate); } catch (exception ex) {system.out.println (ex.getMessage ()); กลับ ""; }} /** รับวันที่ปัจจุบัน* / สตริงคงที่ส่วนตัว getDatastring () {ลอง {simpledateFormat formatter = ใหม่ simpledateFormat ("yyyy-mm-dd"); วันที่ curdate = วันที่ใหม่ (System.currentTimeMillis ()); // รับ Formatter.format.format (Curdate); } catch (exception ex) {system.out.println (ex.getMessage ()); กลับ ""; - 2. เผยแพร่เว็บเซิร์ฟเวอร์ทดสอบ (Eclipse Java EE IDE)
คลิกขวาที่คลาสที่คุณเพิ่งสร้างตอนนี้ WebService-> สร้าง WebService, WebService Type Select Bottom Upjava Bean Web Service, การใช้บริการเลือกคลาสเป้าหมายและขั้นตอนต่อไปเลือกอินเทอร์เฟซวิธีการที่จะเผยแพร่เพื่อเผยแพร่จนกว่าจะเสร็จสมบูรณ์ Tomcat เริ่มต้นแล้ว คลิกขวาที่ไฟล์ WSDL ในโฟลเดอร์ WSDL ที่เพิ่งสร้างขึ้นเลือกเว็บเซอร์วิสทดสอบและป้อนพารามิเตอร์ของวิธีการที่สอดคล้องกัน
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น