• flex+java实现文件的上传


    flex前端代码:

    <?xml version="1.0" encoding="utf-8"?>
    <s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
       xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        width="100%" height="100%" xmlns:component="com.boco.mis.nwcfgdm.component.*"
        creationComplete="init()">

    <fx:Script>
    <![CDATA[

        import com.adobe.net.URI;

        private var file: FileReference; 

        private function init(): void{
            Security.allowDomain("*");
            file = new FileReference();
            file.addEventListener(ProgressEvent.PROGRESS, onProgress);
            file.addEventListener(Event.SELECT, onSelect);
        }

        private function upload(): void{
            file.browse();
        }
        private function onSelect(e: Event): void{
            Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",
            "确认上传",
            Alert.YES|Alert.NO,
            null,
            proceedWithUpload);
        }

        private function proceedWithUpload(e: CloseEvent): void{
            if (e.detail == Alert.YES){
          //采用http向后台发送请求
            var uri:URI = new URI(Application.application.url);

            //获得服务端的ip
            var authority:String = uri.authority;

            //获得服务端的端口号

            var port:String = uri.port;
            var request: URLRequest = new URLRequest("http://"+authority+":"+port+"/nwcfgdm/fileUploadServlet");
            try {
              file.upload(request);
            } catch (error:Error) {
              trace("上传失败");
            }

        }
    }
    //显示进度条
          private function onProgress(e: ProgressEvent): void
          {
            var u:Upload = Upload(PopUpManager.createPopUp(this.parentApplication.parent,Upload,true));
            PopUpManager.centerPopUp(u);
            var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
            u.bar.setProgress(proc, 100);
            u.bar.label= "当前进度: " + " " + proc + "%";
            if(proc == 100){
              u.lbProgress.text="上传完成,正在导入数据...";
              var importDateEvt:ImportDataEvent = new ImportDataEvent();
              CairngormEventDispatcher.getInstance().dispatchEvent(importDateEvt);
            }
    }

    java代码部分:(是一个servelt)

      

    public class FileUploadServlet extends HttpServlet {

      //这里我采用log的形式记录异常信息

        private static final Logger log = LoggerFactory
        .getLogger(NwcfgdmService.class);

        //自定义路径,盘符建议用

         private String uploadPath ="D:"+File.separator+"opt"+File.separator+"BOCO"+File.separator ";

        private static final long serialVersionUID = 1L;

        private int maxPostSize = 100 * 1024 * 1024;

        public FileUploadServlet() {
          super();
        }

        public void destroy() {
          super.destroy();
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

           //防止标题中还有中文字符是出现乱码
            request.setCharacterEncoding("UTF-8");
            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();
                log.error("书写文件出现异常",e);
              }
            }
          }
        }
          catch (FileUploadException e)
        {
          e.printStackTrace();
          log.error("上传文件出现异常",e);

        }
    }

          public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
            doGet(request, response);
           }

          public void init() throws ServletException {
            // Put your code here
          }

    }

    web.xml中的配置:

    <servlet>
      <servlet-name>FileUploadServlet</servlet-name>
      <servlet-class>nwcfgdm.FileUploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>FileUploadServlet</servlet-name>
      <url-pattern>/fileUploadServlet</url-pattern>
    </servlet-mapping>

  • 相关阅读:
    asm createdisk时提示没有权限
    webkit 模拟点击 winform
    .net中的反射(转载)
    wpf——三维学习1
    3dmax导出到blend或者vs中
    地理坐标系与投影坐标系
    Arcgis与CityEngine安装破解
    忘记Windows7登陆密码解决办法
    wpf的UserControl用户控件怎么添加到Window窗体中
    postgres索引创建、 存储过程的创建以及在c#中的调用
  • 原文地址:https://www.cnblogs.com/tianmengwei/p/4980633.html
Copyright © 2020-2023  润新知