RSS
您当前的位置:首页 > SEO资讯

http工具类,访问外网需要使用代理

时间:2023-10-21 12:15:22  来源:  作者:

***Http工具类**{;/**下载文件*文件的网络地址*保存的文件地址*throwsIOException{log.info;if){filetemp.mkdirs;};//文件已经存在拿到加密后文件路径返回FilelocalFilenewFile;if){if0){returnsuccess};};InputStreaminputStreamnull;RandomAccessFilerandomAccessFilenull;//定义文件下载的目录与名称StringpathdownloadDirfileName;//实例化文件对象FilefilenewFile;try{HttpURLConnectionurlConnectionnewURL.openConnection;urlConnection.setRequestMethod;//判断文件路径是否存在if.exists){//如果文件不存在就创建文件file.getParentFile.mkdirs;}intresponseCodeurlConnection.getResponseCode;if{inputStreamurlConnection.getInputStream;intlen0;byte[]datanewbyte[4096];intprogres0;//用于保存当前进度具体进度intmaxProgresurlConnection.getContentLength;//获取文件try{randomAccessFilenewRandomAccessFile;//设置文件大小intunitmaxProgres/100;//将文件大小分成100分每一分的大小为unitintunitProgress0;//用于保存当前进度while)){randomAccessFile.write;progreslen;//保存当前具体进度inttempprogres/unit;//计算当前百分比进度if{//如果下载过程出现百分比变化unitProgresstemp;//保存当前百分比/*log.info;log.info{log.info{randomAccessFile.close;}}}else{log.info{log.info);}finally{if{inputStream.close;}}if){returnHCPClientUtils.getPresignedUrl;}else{returnHCPClientUtils.putFile;}}/**下载文件*paramurlAddress文件的网络地址*paramdownloadDir保存的文件地址*/publicstaticFiledowanloadthrowsIOException{log.info;try{//访问外网需要使用代理//ProxyproxynewProxy);//代理网IP和端口////添加验证信息的请求头格式如下Proxy-AuthorizationBasic//StringheaderKeyProxy-Authorization////组装验证信息//Stringauthqtmp002:Mis09876//StringheaderValueBasicBase64.encodeBase64);//HttpURLConnectionurlConnectionnewURL.openConnection;HttpURLConnectionurlConnectionnewURL.openConnection;urlConnection.setRequestProperty;//ProxyproxynewProxy);//HttpURLConnectionurlConnectionnewURL.openConnection;urlConnection.setRequestMethod;//判断文件路径是否存在if.exists){//如果文件不存在就创建文件file.getParentFile.mkdirs;}intresponseCodeurlConnection.getResponseCode;if{inputStreamurlConnection.getInputStream;intlen0;byte[]datanewbyte[4096];intprogres0;//用于保存当前进度具体进度intmaxProgresurlConnection.getContentLength;//获取文件try{randomAccessFilenewRandomAccessFile;//设置文件大小intunitmaxProgres/100;//将文件大小分成100分每一分的大小为unitintunitProgress0;//用于保存当前进度while)){randomAccessFile.write;progreslen;//保存当前具体进度inttempprogres/unit;//计算当前百分比进度if{//如果下载过程出现百分比变化unitProgresstemp;//保存当前百分比//log.info;log.info{log.info{randomAccessFile.close;}}}else{log.info{//TODOAuto-generatedcatchblocklog.info);}finally{if{inputStream.close;}}returnfile;}/***微信下载文件**paramurlAddress文件url地址*paramdownloadDir文件保存的目录*return文件*/publicstaticFileWXdownloadFile{//调用接口从微信服务器下载对应的图片StringdownloadWxFileURLhttp://file.api.weixin.qq.com/cgi-bin/media/get?access_tokenACCESS_TOKENmedia_idMEDIA_ID//调用客服中心微信公众号后台webservice服务获取ACCESS_TOKENStringaccess_token//获取access_token的webservice接口地址StringtokenwsurlSysProperties.CSCHAT_TOKENWS;//调用webserviceURLurlnull;try{urlnewURL;}catch{log.info);}WexinAccessTokenWebserviceServiceweixinAccessTokenServicenewWexinAccessTokenWebserviceService);WexinAccessTokenWebservicewactServiceweixinAccessTokenService.getWexinAccessTokenWebservicePort;MapString,ObjectrequestContextwactService).getRequestContext;//设置调用webservice超时时间requestContext.put;//TimeoutinmillisrequestContext.put;//Timeoutinmillistry{access_tokenwactService.getWeixinAccessToken{log.info);}//拼装请求URLdownloadWxFileURLdownloadWxFileURL.replace.replace;Filewxfilenull;try{wxfiledowanload;}catch{//TODOAuto-generatedcatchblocklog.info);}returnwxfile;}}

来顶一下
返回首页
返回首页