flex結合servlet檔案上傳
客戶端------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="addListener()"> <mx:Script> <![CDATA[ private var file:FileReference = new FileReference(); private function addListener():void{ file.addEventListener(Event.SELECT,opption); file.addEventListener(Event.COMPLETE,opption); file.addEventListener(ProgressEvent.PROGRESS,opption); file.addEventListener(IOErrorEvent.IO_ERROR,opption); } private function opption(e:Event):void{ if(e.type == Event.SELECT){ filename.text = "選擇了檔案:"+file.name; }else if(e.type == Event.COMPLETE){ filename.text = "上傳完畢"; }else if(e.type == ProgressEvent.PROGRESS){ filename.text = "已上傳: "+Math.round(100*(ProgressEvent(e).bytesLoaded/ProgressEvent(e).bytesTotal))+"%"; }else if(e.type == IOErrorEvent.IO_ERROR){ filename.text = "遠端伺服器未開啟或出現異常,請稍後重試"; } } private function myUpload():void{ var url:String = "http://localhost:8080/flexLogin/servlet/FileUpload"; var urlRequest:URLRequest = new URLRequest(url); file.upload(urlRequest); } ]]> </mx:Script> <mx:Canvas width="436" height="200" backgroundColor="#FFFFFF"> <mx:Text x="10" y="10" text="請選擇要上傳的檔案" fontWeight="bold" fontSize="12" id="filename"/> <mx:Button x="10" y="38" label="瀏覽檔案" fontSize="12" id="search" click="file.browse()"/> <mx:Button x="96" y="40" label="上傳" id="upload" click="myUpload()"/> </mx:Canvas> </mx:Application>
伺服器端---------------------------------------------------------------
package cn.zhangxuemin.Login;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
public FileUpload() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletConfig().getServletContext().getRealPath("/")+"upload\\";
int maxPostSize = 100 * 1024 * 1024;
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
upload.setHeaderEncoding("utf-8");
try {
@SuppressWarnings("unchecked")
List<FileItem> fileItems = upload.parseRequest(request);
Iterator<FileItem> iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(path+name);
try {
item.write(new File(path + name));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
System.out.println(e.getMessage() + "結束");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
}
}
相關文章
- Jsp+Servlet實現檔案上傳下載(一)--檔案上傳JSServlet
- 7.2、使用基於 Servlet 3.0 的檔案上傳Servlet
- 基於servlet的檔案上傳和下載Servlet
- servlet怎樣傳送檔案??Servlet
- php檔案上傳之多檔案上傳PHP
- 檔案上傳漏洞總結(全)
- 單個檔案上傳和批量檔案上傳
- Web檔案上傳方法總結大全Web
- 檔案上傳
- SpringMVC 單檔案上傳與多檔案上傳SpringMVC
- java struts2結合swfupload實現上傳檔案的demo教程Java
- Flex4/Flash多檔案上傳(帶進度條)例項分享Flex
- Java大檔案上傳、分片上傳、多檔案上傳、斷點續傳、上傳檔案minio、分片上傳minio等解決方案Java斷點
- 漏洞重溫之檔案上傳(總結)
- 檔案上傳之三基於flash的檔案上傳
- 前端大檔案上傳/分片上傳前端
- Flask——檔案上傳Flask
- PHP上傳檔案PHP
- JavaScript 檔案上傳JavaScript
- Git上傳檔案Git
- YII檔案上傳
- 檔案上傳概述
- beego上傳檔案Go
- 上傳檔案流程
- 上傳EXCLE檔案
- PHP 檔案上傳PHP
- 檔案上傳漏洞
- MVC檔案上傳 - 使用Request.Files上傳多個檔案MVC
- .NET Core 如何上傳檔案及處理大檔案上傳
- Django檔案上傳 -- 適用於單一小檔案上傳Django
- 上傳檔案的陷阱
- HTTP檔案上傳原理HTTP
- 上傳檔案專題
- PHP 分片上傳檔案PHP
- Java Web 檔案上傳JavaWeb
- WEB漏洞——檔案上傳Web
- Aliyun Oss 上傳檔案
- 使用fileinput上傳檔案