http工具类,访问外网需要使用代理 http工具类,访问外网需要使用代理
创始人
2025-06-15 18:35:36

***Http工具类**{();/**下载文件*文件的网络地址*保存的文件地址*(,,StringfileName,StringfileKey)throwsIOException{log.info(下载中...FilefiletempnewFile(downloadDir);if(!filetemp.exists()){filetemp.mkdirs();};//文件已经存在拿到加密后文件路径返回FilelocalFilenewFile(downloadDirfileName);if(localFile.exists()){if(localFile.length()0){returnsuccess};};InputStreaminputStreamnull;RandomAccessFilerandomAccessFilenull;//定义文件下载的目录与名称StringpathdownloadDirfileName;//实例化文件对象FilefilenewFile(path);try{HttpURLConnectionurlConnection(HttpURLConnection)newURL(urlAddress).openConnection();urlConnection.setRequestMethod(GETurlConnection.setConnectTimeout(10*1000);//判断文件路径是否存在if(!file.getParentFile().exists()){//如果文件不存在就创建文件file.getParentFile().mkdirs();}intresponseCodeurlConnection.getResponseCode();if(responseCode200responseCode300){inputStreamurlConnection.getInputStream();intlen0;byte[]datanewbyte[4096];intprogres0;//用于保存当前进度具体进度intmaxProgresurlConnection.getContentLength();//获取文件try{randomAccessFilenewRandomAccessFile(file,rwdrandomAccessFile.setLength(maxProgres);//设置文件大小intunitmaxProgres/100;//将文件大小分成100分每一分的大小为unitintunitProgress0;//用于保存当前进度(1~100%)while(-1!(leninputStream.read(data))){randomAccessFile.write(data,0,len);progreslen;//保存当前具体进度inttempprogres/unit;//计算当前百分比进度if(temp1tempunitProgress){//如果下载过程出现百分比变化unitProgresstemp;//保存当前百分比/*log.info(正在下载中...unitProgress%*/}}inputStream.close();log.info(下载完成...}catch(Exceptione){log.info(服务器异常...returnfail}finally{if(null!randomAccessFile){randomAccessFile.close();}}}else{log.info(服务器异常...returnfail}}catch(Exceptione){log.info(e.getMessage());}finally{if(null!inputStream){inputStream.close();}}if(HCPClientUtils.doesObjectExist(fileKeyFile.separatorfileName)){returnHCPClientUtils.getPresignedUrl(fileKeyFile.separatorfileName);}else{returnHCPClientUtils.putFile(fileKeyFile.separatorfileName,file,1000*60*60*24*7);}}/**下载文件*paramurlAddress文件的网络地址*paramdownloadDir保存的文件地址*/publicstaticFiledowanload(StringurlAddress,StringdownloadDir,StringfileName)throwsIOException{log.info(下载中...InputStreaminputStreamnull;RandomAccessFilerandomAccessFilenull;//定义文件下载的目录与名称StringpathdownloadDirfileName;//实例化文件对象FilefilenewFile(path);try{//访问外网需要使用代理//ProxyproxynewProxy(Proxy.Type.HTTP,new//InetSocketAddress(10.0.128.40,8080));//代理网IP和端口////添加验证信息的请求头格式如下Proxy-AuthorizationBasic//StringheaderKeyProxy-Authorization////组装验证信息//Stringauthqtmp002:Mis09876//StringheaderValueBasicBase64.encodeBase64(auth.getBytes());//HttpURLConnectionurlConnection(HttpURLConnection)newURL(urlAddress).openConnection(proxy);HttpURLConnectionurlConnection(HttpURLConnection)newURL(urlAddress).openConnection();urlConnection.setRequestProperty(headerKey,headerValue);//ProxyproxynewProxy(Proxy.Type.HTTP,newInetSocketAddress(10.0.60.78/*proxyaddr*/,8080/*proxyport*/));//HttpURLConnectionurlConnection(HttpURLConnection)newURL(urlAddress).openConnection(proxy);urlConnection.setRequestMethod(GETurlConnection.setConnectTimeout(10*1000);//判断文件路径是否存在if(!file.getParentFile().exists()){//如果文件不存在就创建文件file.getParentFile().mkdirs();}intresponseCodeurlConnection.getResponseCode();if(responseCode200responseCode300){inputStreamurlConnection.getInputStream();intlen0;byte[]datanewbyte[4096];intprogres0;//用于保存当前进度具体进度intmaxProgresurlConnection.getContentLength();//获取文件try{randomAccessFilenewRandomAccessFile(file,rwdrandomAccessFile.setLength(maxProgres);//设置文件大小intunitmaxProgres/100;//将文件大小分成100分每一分的大小为unitintunitProgress0;//用于保存当前进度(1~100%)while(-1!(leninputStream.read(data))){randomAccessFile.write(data,0,len);progreslen;//保存当前具体进度inttempprogres/unit;//计算当前百分比进度if(temp1tempunitProgress){//如果下载过程出现百分比变化unitProgresstemp;//保存当前百分比//log.info(正在下载中...unitProgress%}}inputStream.close();log.info(下载完成...}catch(Exceptione){log.info(服务器异常...returnnull;}finally{if(null!randomAccessFile){randomAccessFile.close();}}}else{log.info(服务器异常...returnnull;}}catch(Exceptione){//TODOAuto-generatedcatchblocklog.info(e.getMessage());}finally{if(null!inputStream){inputStream.close();}}returnfile;}/***微信下载文件**paramurlAddress文件url地址*paramdownloadDir文件保存的目录*return文件*/publicstaticFileWXdownloadFile(StringdownloadDir,StringfileName,StringserverId){//调用接口从微信服务器下载对应的图片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(tokenwsurl);}catch(MalformedURLExceptione){log.info(e.getMessage());}WexinAccessTokenWebserviceServiceweixinAccessTokenServicenewWexinAccessTokenWebserviceService(url,newQName(http://ws.wxpp.gclife.com/,WexinAccessTokenWebserviceService));WexinAccessTokenWebservicewactServiceweixinAccessTokenService.getWexinAccessTokenWebservicePort();MapString,ObjectrequestContext((BindingProvider)wactService).getRequestContext();//设置调用webservice超时时间requestContext.put(com.sun.xml.internal.ws.connect.timeout,1000*120);//TimeoutinmillisrequestContext.put(com.sun.xml.internal.ws.request.timeout,1000*120);//Timeoutinmillistry{access_tokenwactService.getWeixinAccessToken(}catch(Exceptione1){log.info(e1.getMessage());}//拼装请求URLdownloadWxFileURLdownloadWxFileURL.replace(MEDIA_ID,serverId).replace(ACCESS_TOKEN,access_token);Filewxfilenull;try{wxfiledowanload(downloadWxFileURL,downloadDir,fileName);}catch(Exceptione){//TODOAuto-generatedcatchblocklog.info(e.getMessage());}returnwxfile;}}

 

相关阅读

  • 使用代理连外网代理接外网服务器
  • linux内网机器访问外网代理设置
  • 外网代理服务器
  • 外网代理
  • linux内网机器访问外网代理设置
  • 使用代理连外网代理接外网服务器
  • http工具类,访问外网需要使用代理
  • 分享到: QQ空间 新浪微博 腾讯微博 人人网 微信 百度 复制网址

    相关内容

    热门资讯

    宁波精达(603088.SH)... 格隆汇12月18日丨宁波精达(603088.SH)公布,公司于2025年12月18日收到股东郑功出具...
    传壁仞科技拟圣诞节前启动港股I... 观点网讯:12月18日,国产GPU企业壁仞科技计划于圣诞节前启动港股IPO程序,拟集资5亿至6亿美元...
    黑牡丹(600510.SH)子... 黑牡丹(600510.SH)发布公告,近日,公司全资子公司常州黑牡丹置业有限公司以总价 6.77亿元...
    豆包大模型联合润欣科技、老凤祥...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:IT之家)I...
    万物云回购24万股 总金额46... 万物云(02602)发布公告,2025年12月18日,公司回购股份24万股,回购金额为461万港元。...