---》
Flex
class="xml"><?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" fontSize="12" creationComplete="init()">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.events.*;
import mx.collections.ArrayCollection;
import mx.collections.ArrayList;
import mx.controls.Alert;
import mx.core.FlexGlobals;
private var files : FileReferenceList;
[Bindable]
private var fileArray : ArrayList;
private var serverUrl : String;
private var userId : String;
private var allowTypes : XML;
private var xlsFilter:FileFilter = new FileFilter("定值单", "*.xls;*.xlsx");
private var setFileReference:FileReferenceList = new FileReferenceList();
private var request:URLRequest=new URLRequest("http://localhost:8080/fileTest/upload/servlet/FileUploadServlet");
[Bindable]
private var resultArr:ArrayCollection = new ArrayCollection();
private function init() : void {
Security.allowDomain("*");
setFileReference.addEventListener(Event.SELECT, onSelect);
}
private function upload() : void {
setFileReference.browse([xlsFilter]);
}
private function onSelect(e:Event) : void {
fileArray = new ArrayList(setFileReference.fileList);
for(var i:int = 0; i < fileArray.length; i++) {
var file:FileReference = fileArray.source[i] as FileReference;
file.addEventListener(Event.COMPLETE,onComplete);
file.upload(request);
}
}
private function onComplete(event:Event):void{
// Alert.show((event.target as FileReference).name+"上传完成!");
var obj:Object = new Object();
obj.result = (event.target as FileReference).name+"上传完成!";
resultArr.addItem(obj);
}
]]>
</fx:Script>
<s:VGroup>
<s:Button label="上传文件" click="upload();" />
<s:List id="list" dataProvider="{resultArr}" labelField="result" width="250"/>
</s:VGroup>
</s:Application>
---》Servlet
需要commons-fileupload.jar commons-io.jar
package servlet;
import java.io.File;
import java.io.IOException;
import java.util.Date;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadServlet extends HttpServlet {
// 定义文件的上传路径
private String uploadPath = "c://";
// 限制文件的上传大小
private static final long maxFileSize = 104857600;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
Date date = new Date();
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxFileSize);
upload.setHeaderEncoding("UTF-8");
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
// 依次处理每一个文件:
String dictId = request.getParameter("dictId");
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String fileName = item.getName();
long fileSize = item.getSize();
if (fileName != null && fileName.trim().length() > 0 && fileSize < maxFileSize) {
// 取得上传文件后缀名
// int index=fileName.lastIndexOf(".");
// String docType = fileName.substring(index);
// String docName=fileName.substring(0,index);
File dir=new File(getServletContext().getRealPath("upload/template/"));
if(!dir.exists()){
dir.mkdirs();
}
String filePath = "upload/template/" + fileName;
// String filePath = "upload/template/" + date.getTime() + docType;
File uploadFile = new File(getServletContext().getRealPath(filePath));
item.write(uploadFile);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
---》参考
http://blog.csdn.net/huangssssx/article/details/5785801#
http://javacrazyer.iteye.com/blog/707693