【JavaEE】邮箱附件下载(whitelabel Error Page)

本文介绍了一个具体的邮箱附件下载实现过程,包括使用Java进行登录验证、获取邮件详情、下载附件等关键步骤,并分享了一些实践中的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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是用来将路径映射成非项目内保存附件的路径;

failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: failed to req API:127.0.0.1:8848/nacos/v1/ns/instance. code:403 msg: <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Wed Jun 25 15:26:48 CST 2025</div><div>There was an unexpected error (type=Forbidden, status=403).</div><div>unknown user!</div></body></html> at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:467) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:389) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:191) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:207) ~[nacos-client-1.1.4.jar:na] at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:64) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:76) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at com.atguigu.yygh.hosp.ServiceHospApplication.main(ServiceHospApplication.java:16) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE] Process finished with exit code 1
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶洲川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值