本文实例为大家分享了 Java 实现文件上传的具体代码 , 具体内容如下
1 、 java : :
حزمة com.github.reston.servlet ؛ استيراد java.io.file ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ استيراد java.io.printwriter ؛ import java.util.iterator ؛ import java.util.list ؛ استيراد javax.servlet.servletconfig ؛ استيراد javax.servlet.servletexception ؛ import javax.servlet.annotation.webservlet ؛ import javax.servlet.http.httplet ؛ استيراد org.apache.commons.fileupload.fileItem ؛ import org.apache.commons.fileupload.fileItemFactory ؛ import org.apache.commons.fileupload.disk.diskfileitemfactory ؛ org.apache.commons.io.ioutils ؛ @webservlet ("/ajaxupload") الفئة العامة Ajaxupload يمتد httpservlet {Override public void init (servletconfig config) يلقي servletexception {// tode method method method clud.init (config) ؛ } Override Service Void Service (طلب httpservletrequest ، استجابة httpservletresponse) يلقي ServleTexception ، ioException {response.setContentType ("text/html") ؛ request.setcharacterencoding ("UTF-8") ؛ Boolean Ismultipart = servletfileupload.ismultipartContent (request) ؛ String BasePath = getServletContext (). getRealPath ("/expload") ؛ File riptirectory = ملف جديد (BasePath) ؛ اسم ملف السلسلة = "" ؛ بداية طويلة = 0 ؛ if (! riptirectory.isdirectory ()) riptiredirectory.mkdirs () ؛ if (isMultiPart) {try {fileItemFactory Factory = new DiskfileItemFactory () ؛ servletfileupload تحميل = جديد servletfileupload (المصنع) ؛ suppressWarnings ("uncheced") قائمة <IbleItem> fileItems = Upload.ParsereQuest (request) ؛ لـ (fileItem I: fileItems) {if (i.isformfield ()) {string name = i.getFieldName () ؛ قيمة السلسلة = i.getString () ؛ if (name.equals ("start")) start = long.parselong (i.getString ()) ؛ }} لـ (fileItem item: fileItems) {if (item.isformfield ()) متابعة ؛ filename = item.getFieldName () ؛ if (mkdir (basePath)) {file fileOnServer = createFile (basepath ، filename) ؛ if (fileOnServer.length () == 0) {fileOutputStream fos = new FileOutputStream (fileonserver ، true) ؛ ioutils.copy (item.getInputStream () ، fos) ؛ } if (start> 0) {fileOutputStream fos = newOutputStream (fileonserver ، true) ؛ ioutils.copy (item.getInputStream () ، fos) ؛ } printWriter pw = response.getWriter () ؛ pw.write ("{/" length/":/" "+fileonserver.length ()+"/"}") ؛ pw.flush () ؛ }}} catch (استثناء e) {}}} ملف خاص createFile (مسار السلسلة ، اسم السلسلة) يلقي ioException {file tmp = new file (path ، name) ؛ if (! tmp.exists ()) {tmp.createNewFile () ؛ } إرجاع TMP ؛ } boolean mkdir الخاص (مسار السلسلة) {boolean result = true ؛ ملف tmp = ملف جديد (path) ؛ if (! tmp.isdirectory ()) {result = tmp.mkdirs () ؛ } نتيجة الإرجاع ؛ }}2 、 java : :
var ajaxupload = function (e) { / ** * e url method method success error * / var xmlhttprequest ؛ if (window.xmlHttPrequest) {xmlHttPrequest = new xmlHttprequest () ؛ if (xmlhttprequest.overridemiMetype) {xmlHttPrequest.overridemiMetype ("text/xml") ؛ }} آخر if (window.activexobject) {var activename = ["msxml2.xmlHtp" ، "microsoft.xmlhttp"] ؛ لـ (var i = 0 ؛ i <activename.length ؛ i ++) {try {xmlHttPrequest = new ActiveXObject (Activename [i]) ؛ استراحة؛ } catch (e) {return ؛ }}} if (xmlhttprequest == undefined || xmlhttprequest == null) {Alert ("xmlhttprequest 对象创建失败!!") ؛ يعود؛ } آخر {this.xmlHttp = xmlhttprequest ؛ } var file = document.getElementById (E.ID) ؛ if (this.xmlHttp! = undefined && this.xmlhttp! = null) {e.method = e.method.touppercase () ؛ if ( يعود؛ } if (e.url == null || يعود؛ }} this.xmlHttp.OnReadyStateChange = function () {if (this.readyState == 4) {if (this.status == 200) {var besponsetext = this.responsetext ؛ var responsexml = this.reponsexml ؛ if (eSuccess == undefined || تنبيه (": :" + replectext) ؛ } else {e.success (besponseText ، responsexml) ؛ }} else {if (e.error == undefined || التنبيه ("http 的响应码 :" + this.status + "، 响应码的文本信息 :" + this.statustext) ؛ } آخر {e.error (this.status ، this.statustext) ؛ }}}} // var formhtm = "<form id = 'output' enctype = 'multipart/form-data'> </form>" ؛ var filename = getFilename (e.id) ؛ this.xmlhttp.open (e.method ، e.url ، true) ؛ var data = new formData (document.getElementById ("output")) ؛ data.append ("الاسم" ، اسم الملف) ؛ data.append ("start" ، e.data.start) ؛ data.append (filename ، document.getElementById (E.ID) .files [0] .slice ( this.xmlhttp.send (data) ؛} وظيفة getFilename (id) {var path = document.getElementById (id) .value var pos1 = path.lastindexof ('/') ؛ var pos2 = path.lastindexof ('//') ؛ var pos = math.max (pos1 ، pos2) ؛ return path.substring (pos + 1) ؛} وظيفة getFilesize (id) {return document.getElementById (id) .files [0] .Size ؛}3 、 HTML 代码 :
<! doctype html> <html> <head> <meta http-equiv = "content-type" content = "text/html ؛ charset = utf-8"> <script type = "text/javaScript" src = "test.js"> </script> </body> value = "上传"/> <span> 请选择要上传的文件 (小于 1g) </span> <input type = "button" value = "上传" onClick = "Test () ؛"/> <form id = "output" enved = "multipart/form-data"> </form> <script> test () "/PCC/RESTON/AJAXUPLOAD" ، الطريقة: "post" ، البيانات: {start: 0} ، النجاح: الدالة (e) {var l = json.parse (e) .Length ؛ } error: function (e) {console.log (e) ؛ } </script> </body> </html>以上就是本文的全部内容 , 希望对大家学习 java 程序设计有所帮助。