Flex:
FileUpload.mxml
Application
java Servelet :需要common-io,commons-fileupload包
seam 组件
配置文件:
web.xml中添加
components.xml 添加
FileUpload.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import com.gptri.core.GlobalParam;
import flash.net.FileReference;
import com.gptri.gpweb.component.StatusEnum;
import org.granite.tide.events.TideUIEvent;
private var fileRef:FileReference =new FileReference();;
private var gp:GlobalParam=GlobalParam.getInstance();
public function get fileName():String
{
return fileRef.name;
}
private function fileBrowse():void
{
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
var gifFilter:FileFilter=new FileFilter("PowerPoint", "*.ppt", "PPT");
fileRef.browse([gifFilter]);
}
private function onProgress(e:ProgressEvent):void
{
var proc:uint=e.bytesLoaded / e.bytesTotal * 100;
dispatchEvent(new TideUIEvent("statusChange", "当前进度: " + " " + proc + "%", StatusEnum.WORKING));
}
private function selectHandler(event:Event):void
{
var request:URLRequest=new URLRequest(gp.sysHost + "FileUploadServlet");
fileRef=FileReference(event.target);
fileRef.upload(request);
}
private function completeHandler(event:Event):void
{
dispatchEvent(new Event("uploadComplete"));
}
]]>
</mx:Script>
<mx:Button click="fileBrowse()" label="上传文件" fontSize="12"/>
</mx:Canvas>
Application
public function init():void
{
fileUpload.addEventListener("uploadComplete", uploadComplete);
}
[Observer("uploadComplete")]
public function uploadComplete(e:Event):void
{
weekReportSearch.uploadPPT(uploadPPTResult);
}
private function uploadPPTResult(e:TideResultEvent):void
{
dispatchEvent(new TideUIEvent("statusChange", e.result as String, StatusEnum.WORKING));
CursorManager.removeBusyCursor();
}
<cs:FileUpload id="fileUpload"/>
java Servelet :需要common-io,commons-fileupload包
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 FileUploadServlet extends HttpServlet {
// 定义文件的上传路径
private String uploadPath = "D:\\upload\\";
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
public FileUploadServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 保存文件到服务器中
response.setContentType("text/html; charset=UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
request.getSession().setAttribute("uploadFile",
uploadPath + name);
System.out.println(name);
try {
item.write(new File(uploadPath + name));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
seam 组件
@Name("weekReportSearch")
@Scope(EVENT)
@TideEnabled
public class WeekReportSearchingAction {
@In
private EntityManager entityManager;
@In(required = false)
private String uploadFile;
public String uploadPPT() {
//SlideShow ppt = new SlideShow(new HSLFSlideShow(uploadFile));
// Put your code here
}
配置文件:
web.xml中添加
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.gptri.gpweb.service.util.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/FileUploadServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Seam Multipart Filter</filter-name>
<filter-class>org.jboss.seam.web.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Multipart Filter</filter-name>
<url-pattern>*.seam</url-pattern>
</filter-mapping>
components.xml 添加
<web:multipart-filter create-temp-files="true"
max-request-size="104857600"
url-pattern="*.seam"/>