【JavaEE】郵箱附件下載(whitelabel Error Page)__Java

來源:互聯網
上載者:User
1.問題

最近在做郵箱裡面的附件下載,搞得是頭昏腦脹,現在把遇到的困難總結下; 2.原始碼

public EmailDetail selectEmailReceivedDetail(String mid, OtherSystemConfig otherSystemConfig,Integer fid){        EmailLogin emailLogin = new EmailLogin();//①建立登入,登入後擷取連結中參數        EmailReceivedDetailConn emailReceivedDetailConn = new EmailReceivedDetailConn();//②郵件詳情登入,擷取郵件詳情頁面的json,然後解析擷取        RequestContext requestContext = emailLogin.login(otherSystemConfig);//③使用者登入,擷取本使用者下的上下文;        requestContext.globalData("mid",mid);        requestContext.globalData("fid",fid);        //④把mid和fid放到請求上下文中的globalData中去;        emailReceivedDetailConn.post(requestContext);//發送請求上下文        EmailDetail emailDetail= (EmailDetail) requestContext.globalData("emailReceivedDetail");//從請求內容相關的globalData中擷取emailReceivedDetail實體物件;        String urlAtt=String.valueOf(requestContext.globalData("url"));//不含sid的url;        for (Attachments a: emailDetail.getAttachments()){        //遍曆emailDetail實體類中的Attachments實體類數組;         //String nameEncoded= URLEncoder.encode(a.getFileName(),"UTF-8");這裡不能放出來            String location = uploadPath + "/mail/send/"+mid+"/"+a.getFileName();             //這裡a.getFileName不能寫nameEncoded,不然會報white            //檔案的路徑            String outputFolder = uploadPath+"/mail/send/"+mid;            //檔案所在檔案夾的路徑            Connection.Response response = null;            try{                response= Jsoup.connect(urlAtt+"&sid="+requestContext.globalData("sid")).cookies(requestContext.cookies()).ignoreContentType(true).execute();//含有sid的url,帶著cookies和ignoreContentType(true)去擷取響應            }catch (Exception e){                e.printStackTrace();            }            //檔案輸出資料流            FileOutputStream out = null;            try{                //檔案夾                File dir = new File(outputFolder);                if (!dir.exists()){                //如果檔案夾不存在,就建立一個檔案夾(以mid為檔案夾名字)                    dir.mkdirs();//建立檔案夾                }                File file = new File(location);//檔案路徑對應的檔案                out=(new FileOutputStream(file));//讀取檔案輸出                out.write(response.bodyAsBytes());//寫出                out.close();//關閉流            }catch (Exception e){                e.printStackTrace();            }            a.setUrl(parentUrl+"/upload/mail/send/"+mid+"/"+a.getFileName());            //這裡a.getFileName不能寫nameEncoded,不然會報white            //設定emaiDetail中的attachments中的url;        }        return emailDetail;//返回emailDetail實體類對象    }
3.截圖

重複 4.總結

①這裡,不需要對url進行URLEncoder編碼,否則會報錯;如果你報如下錯誤,可能是這個原因:

②attachments附件實體類是被emailDetail包含在其中的,attachments中設定url時(即穩重for迴圈裡面的a),一定要在流關閉之後;
③注意,requestContext上下文中的globalData的作用範圍,在同一次請求中,都可以拿得到,如果發起新的請求,requestContext就會更新,就拿不到其中的值了,這裡,requestContext是以索引值對的形式來儲存的,如果索引值對都寫,那是寫入儲存;如果唯寫了鍵,那是讀取,取出;
④這裡的parentUrl是自己項目的scheme+ip地址;
⑤這裡的uploadPath是用來將路徑映射成非項目內儲存附件的路徑;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.