Problems with using filereference for flex and Java uploads

Source: Internet
Author: User
Tags filefactory

During this time, we will upload files because flex cannot communicate with Java by using blazeds to transmit information. In addition, once the files are large, the memory must be large enough,

Therefore, we have to give up the blazeds method.

Use the Flex + serverlet Method for easy transfer.

Encoding Problems:

1 is solved with urlencoding (Tomcat class ),

2. When Flex is passed over, the file is saved as the header interpretation content file. Obviously, this is not the case. A lot of information has been found.

See materials: http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#upload

:

 POST /handler.cfm HTTP/1.1   Accept: text/*  Content-Type: multipart/form-data;   boundary=----------Ij5ae0ae0KM7GI3KM7   User-Agent: Shockwave Flash   Host: www.example.com   Content-Length: 421   Connection: Keep-Alive   Cache-Control: no-cache    ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  Content-Disposition: form-data; name="Filename"    MyFile.jpg  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  Content-Disposition: form-data; name="Filedata"; filename="MyFile.jpg"  Content-Type: application/octet-stream    FileDataHere  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  Content-Disposition: form-data; name="Upload"    Submit Query  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7--

It is a file in this format, so the file content must be parsed before it can be obtained,

I have thought about writing a parsing class manually, but I have analyzed this structure and it seems that it is not a good parsing. So there is no way to do this, but in the first analysis, this structure is a bit like the file structure of Apache's open-source Upload Component,

Org. Apache. commons. fileupload. servlet. servletfileupload class,

These two packages should be required and can be used after testing.

C: \ Users \ yjy \. m2 \ repository \ com \ huadi \ cscp \ commons-fileupload \ 1.2.2 \ commons-fileupload-1.2.2.jar

C: \ Users \ yjy \. m2 \ repository \ com \ huadi \ cscp \ commons-IO \ 1.4 \ commons-io-1.4.jar

Below is the code sharing. The focus here is the Servlet's code:

Package com.gz HDI. cscp. service; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. f Ileupload. defaultfileitemfactory; import Org. apache. commons. fileupload. fileitem; import Org. apache. commons. fileupload. fileitemfactory; import Org. apache. commons. fileupload. fileuploadexception; import Org. apache. commons. fileupload. servlet. servletfileupload; import Org. apache. log4j. logger; import com.gz HDI. cscp. API. comm. def. cdmirequestdefine; import com.gz HDI. CSMP. gate. dataoperate; @ suppresswarnings ("deprec Ation ") public class updownfff extends httpservlet {Private Static final long serialversionuid = 1l; Private Static final logger = logger. getlogger (updownfff. class); dataoperate op = new dataoperate (null, null); public static final string method_post = "Post"; public static final string method_get = "get "; /*** @ see httpservlet # httpservlet () */Public updownfff () {super ();}/*** it means what I mean by your request. Send * @ see httpservlet # doget (httpservletrequest request, httpservletresponse * response) */protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {logger.info ("Download start ....... "); string uri = request. getpathinfo (); inputstream in = op. httpactionforinputstream (method_get, Uri + "? "+ Request. getquerystring (), false); outputstream out = response. getoutputstream (); int n = 0; byte [] B = new byte [1024]; while (n = in. read (B ))! =-1) {out. write (B, 0, n);} In. close (); out. close (); logger.info ("when the file is downloaded from the client:" + system. currenttimemillis (); logger.info ("Download end ....... ");}/**** is designed to be used uniformly by the proxy */protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {logger.info ("Upload start ....... "); Request. setcharacterencoding ("UTF-8"); string uri = new string (request. getpathinfo (). Getbytes ("iso8859-1"), "UTF-8") + "? "+ Request. getquerystring (); Request. getparameter ("Upload"); inputstream in = NULL; try {fileitemfactory filefactory = new defaultfileitemfactory (); // parse the Request body to retrieve the file body servletfileupload upload = new servletfileupload (filefactory ); @ suppresswarnings ("unchecked") List <fileitem> fileitems = upload. parserequest (request); iterator <fileitem> iter = fileitems. iterator (); fileitem = NULL; while (ITER. hasnext ()){ Fileitem = ITER. Next (); If (! Fileitem. isformfield () {In = fileitem. getinputstream () ;}} catch (fileuploadexception e) {e. printstacktrace ();} string Re = op. proxyhttpaction (method_post, Uri, In, null); outputstream out = response. getoutputstream (); out. write (Re. getbytes (); out. close (); // test // inputstream senddatastream = request. getinputstream (); // int n = 0; // int COUNT = 0; // byte [] B = new byte [1024]; // while (n = senddatastrea M. Read (B ))! =-1) // {// count + = N; // system. out. print ("recevie:" + new string (B, 0, n); //} // logger.info ("Upload end ....... ");}/*** assemble the request header using * @ Param Request * @ author yinlei | 4:21:59 * @ version 0.1 */Public Map <string, string> getheaders (httpservletrequest request) {Map <string, string> map = new hashmap <string, string> (); string Sign = request. getheader (cdmirequestdefine. header_sign); If (sign! = NULL) {map. Put (cdmirequestdefine. query_appid, sign);} return map ;}}

Can see this, this brother wrote in detail, thank you very much: http://www.blogjava.net/rainwindboys/archive/2008/09/18/229219.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.