flex 上传文件 HTTP方式上传文件、…

 

Flex 文件上传—web前端

      项目上用到文件上传的界面,没学过flash编程,从网上找的资料初步实现了功能,整理如下:

1.      上传界面如下:

2.      整合flashMyeclipse

(1)     首先下载Flash Builder4插件,迅雷下载地址:http://trials.adobe.com/AdobeProducts/FLBR/4/win32/FlashBuilder_4_Plugin_LS10.exe

2)双击下载的.exe文件

3)选择Flash Builer4安装的路径,注意这里的安装路径一定不能包含中文,否则会安装失败。



4)选择MyEclipse8.5的安装路径

plug into another copy of Eclipse 项选择你自己myEclipse的路径
5)安装完成之后,打开MyEclispe会报一个错误,不要管它,重启一下MyEclipse就可以了。

3.从网上下载的有关flash上传的例子,源代码如下:

该文件为扩展名为:.mxml;建立flash工程,然后运行会生成两个文件:flexUploadFile.htmlflexUploadFile.swf;文件;注:普通网页直接通过<object>对象引用.swf文件时flash打不开的情况,解决方式如下:在flash工程上右键àpropertiesàflex build path à library path àframeword linkage(修改编码方式为:Merged into code 原因不知);

4.java后台(通过:public varurlRequest:URLRequest=new URLRequest("/PESBTS/file/flexUploadFile_uploadFile.action");  //服务器类地址

发送请求,请求后台Action;

    注:var fileReference:FileReference=FileReference(fileArrayCollction[i]['fileRefer']);

                 

fileReference.upload(urlRequest);

fileReference采用此默认的写法的话后台需用privateList<File> Filedata;

    // 文件名

    privateList<String> FiledataFileName;

    // 文件内容类型

    privateList<String> FiledataContentType;方式接受想应的参数!

5 通过HTTP方式请求上传到hfs文件服务器方法:upload(List<String> list, List<String> nameList, String desAddress) {

       // 要上传的文件名,如:d:\haha.doc.你要实现自己的业务。我这里就是一个空list.

       try {

           String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线

           URL url = newURL(desAddress);

           HttpURLConnection conn = (HttpURLConnection) url.openConnection();

           // 发送POST请求必须设置如下两行

           conn.setDoOutput(true);

           conn.setDoInput(true);

           conn.setUseCaches(false);

           conn.setRequestMethod("POST");

           conn.setRequestProperty("connection", "Keep-Alive");

           // conn.setRequestProperty("user-agent",

           // "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

           conn.setRequestProperty("Charsert", "UTF-8");

           conn.setRequestProperty("Content-Type",

                  "multipart/form-data; boundary=" + BOUNDARY);

           OutputStream out = new DataOutputStream(conn.getOutputStream());

           byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线

 

           int leng = list.size();

           for (int i = 0; i < leng; i++) {

              String fname = list.get(i);

              String realName = nameList.get(i);

              //System.out.println(fname);

              File file = new File(fname);

              StringBuilder sb = new StringBuilder();

              sb.append("--");

              sb.append(BOUNDARY);

              sb.append("\r\n");

              sb.append("Content-Disposition: form-data;name=\"file" + i

                     + "\";filename=\"" + realName + "\"\r\n");

              sb.append("Content-Type:application/octet-stream\r\n\r\n");

              byte[] data = sb.toString().getBytes();

              out.write(data);

              DataInputStream in = new DataInputStream(new FileInputStream(

                     file));

              int bytes = 0;

              byte[] bufferOut = new byte[1024];

              while ((bytes = in.read(bufferOut)) != -1) {

                  out.write(bufferOut, 0, bytes);

              }

              out.write("\r\n".getBytes()); // 多个文件时,二个文件之间加入这个

              in.close();

           }

           out.write(end_data);

           out.flush();

           out.close();

           // 定义BufferedReader输入流来读取URL的响应

           BufferedReader reader = new BufferedReader(new InputStreamReader(

                  conn.getInputStream()));

           String line = null;

           while ((line = reader.readLine()) != null) {

           }

           return true;

       } catch (Exception e) {

           //System.out.println("发送POST请求出现异常!" + e);

           e.printStackTrace();

           return false;

       }

删除的方法:

 

    public static voiddeleteFile(String fileUrl,String fileName){

    //  String qq = httpFile.getName();

       String str = "action=delete&selection="+fileName+"";

       URL url;

       try {

           url = newURL(fileUrl);

           HttpURLConnection con;

           con = (HttpURLConnection)url.openConnection();

           con.setRequestMethod("POST");

           con.setDoOutput(true);

           OutputStream out = con.getOutputStream();

           out.write(str.getBytes());

           InputStream in = con.getInputStream();

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值