"Java EE" mailbox attachment download (Whitelabel Error Page) __java

Source: Internet
Author: User
1. The question

Recently in the mailbox inside the attachment download, make it is a headache, now put the difficulties summed up; 2. Source Code

Public Emaildetail Selectemailreceiveddetail (String Mid, othersystemconfig Othersystemconfig,integer FID) {EmailLo Gin Emaillogin = new Emaillogin ();//① Create login, get parameters in link after login emailreceiveddetailconn emailreceiveddetailconn = new Emailre Ceiveddetailconn ();//② mail details Log in, get the Message Details page JSON, and then parse get requestcontext RequestContext = Emaillogin.login (othersystemconf
        IG);//③ user Login, get the context under this user, Requestcontext.globaldata ("mid", mid);
        Requestcontext.globaldata ("FID", FID); ④ the mid and fid into the GlobalData in the context of the request; Emailreceiveddetailconn.post (RequestContext)/Send request Context Emaildetail email Detail= (Emaildetail) requestcontext.globaldata ("Emailreceiveddetail"); Gets the Emailreceiveddetail entity object from the GlobalData of the request context; String urlatt=string.valueof (requestcontext.globaldata ("url");
         A URL that does not contain a SID; for (Attachments a:emaildetail.getattachments ()) {//Traverse an array of Attachments entity classes in the Emaildetail entity class; String nameencoded= Urlencoder.encode (A.getfilename (), "UTF-8");
            String location = Uploadpath + "/mail/send/" +mid+ "/" +a.getfilename (); Here A.getfilename cannot write nameencoded, otherwise will report white//file path String OutputFolder = uploadpath+ "/mail/send/" +m
            Id
            The path to the folder where the file resides connection.response Response = null; try{response= jsoup.connect (urlatt+ "&sid=" +requestcontext.globaldata ("Sid"). Cookies (requestcontext.c Ookies ()). Ignorecontenttype (True). Execute ();//The URL containing the SID, with cookies and Ignorecontenttype (true) to get the response}catch (excepti
            On e) {e.printstacktrace ();

            }//File output stream FileOutputStream out = null;
                try{//Folder File dir = new file (OutputFolder);
                if (!dir.exists ()) {//If the folder does not exist, create a new folder (with mid as the folder name) Dir.mkdirs (); {File File = new location;//file path corresponds to the files out= (new fileoutputsTream (file);//Read Out.write (Response.bodyasbytes ());//write Out.close ();//Close stream
            }catch (Exception e) {e.printstacktrace ();
            } a.seturl (parenturl+ "/upload/mail/send/" +mid+ "/" +a.getfilename ()); Here A.getfilename cannot write nameencoded, otherwise will report white//Set the URL in the attachments in Emaidetail; return Emaildet ail;//return Emaildetail entity class object}
3. Screenshot

Repeat 4. Summary

① here, you do not need to urlencoder the URL code, otherwise it will be an error, if you report the following errors, may be the reason:

②attachments attachment entity class is included in the Emaildetail, attachments set the URL (that is, the steady for loop inside a), must be after the flow closed;
③ Note that the scope of the GlobalData in the RequestContext context is available in the same request, and if a new request is made, RequestContext is updated, and the value is not taken, here, The RequestContext is saved as a key-value pair, and if the key-value pairs are written, that is write-save; If only the key is written, it is read, fetched;
④ here the ParentURL is the SCHEME+IP address of its own project;
⑤ here the Uploadpath is used to map the path to the path to save the attachment in a non project;

Related Article

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.