以服务于中国广大创业者为己任,立志于做最好的创业网站。

标签云创业博客联系我们

导航菜单

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

***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;}}