Flex上传文件功能

upload.mxml

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute" width="497" height="136" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#F2F8F8, #45E7E5]">
    <mx:Script source="upload.as"></mx:Script>
    <mx:Style>
        .myfont{font-size:13pt}
    </mx:Style>
    <mx:Button x="10" y="95" label="上传文件" click="pickfile()" styleName="myfont"/>
    <mx:Label x="10" y="10" text="文件上传" styleName="myfont"/>
    <mx:ProgressBar x="10" y="40" width="457" themeColor="#F20D7A" minimum="0" mode="manual" maximum="100" id="progress1" label="当前进度: 0%" styleName="myfont" fontWeight="normal"/>
    <mx:Label x="146" y="98" width="321" id="lbProgress" styleName="myfont" textAlign="right"/>
</mx:Application>

 

 

upload.as

 

// ActionScript file
  import flash.events.Event;
  import flash.net.FileFilter;
  import flash.net.FileReference;
  private var fileRef:FileReference = new FileReference();
  private function init():void{
      
  }
 
 private function pickfile():void{
     var imageTypes:FileFilter = new FileFilter("图片 (*.jpg, *.jpeg, *.gif,*.png)", "*.jpg; *.jpeg; *.gif; *.png");
     var textTypes:FileFilter = new FileFilter("文本文件(*.txt","*.txt;");
     var officeType:FileFilter = new FileFilter("Office文件(*.doc, *.xls","*.doc; *.xls");
     var anyType:FileFilter = new FileFilter("所有文件(*.*)","*.*");
    var allTypes:Array = new Array(imageTypes, textTypes,officeType,anyType);
     fileRef.addEventListener(Event.SELECT, selectHandler);
     fileRef.addEventListener(Event.COMPLETE, completeHandler);
     fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    fileRef.addEventListener("ioError", ioerrorHandler);
    try{
        var success:Boolean = fileRef.browse(allTypes);
     }catch (error:Error){
         trace("Unable to browse for files."+error.toString());
     }
 }
 private function ioerrorHandler(event:Event):void{
     trace("Unable to upload file."+event.toString());
 }
 private function progressHandler(event:ProgressEvent):void{
     lbProgress.text = " 已上传 " + (event.bytesLoaded/1024).toFixed(2)+ " K,共 " + (event.bytesTotal/1024).toFixed(2) + " K";
     var proc: uint = event.bytesLoaded / event.bytesTotal * 100;
    progress1.setProgress(proc, 100);
     progress1.label= "当前进度: " + " " + proc + "%";
 
 }
 private    function selectHandler(event:Event):void{
    var request:URLRequest = new URLRequest("http://localhost:9080/upload/upload.jsp")
     try
     {
        fileRef.upload(request);
    }
     catch (error:Error)
   {
         trace("Unable to upload file."+error.toString());
     }
 }
 private function completeHandler(event:Event):void{
     trace("uploaded");
 }
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FileUploadServlet.class package test; 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 = "G://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 { // 得到用户需要保存的服装的id //String dressId = request.getParameter("dressID"); //System.out.println(dressId); // 保存文件到服务器中 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(); 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 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值