大家好,感谢邀请,今天来为大家分享一下验证网站源码分享的问题,以及和检测网站源码的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
以下为java下载网络文件的实例,可支持断点继续下载:
importorg.apache.http.Header;\nimportorg.apache.http.HttpEntity;\nimportorg.apache.http.auth.AuthScope;\nimportorg.apache.http.auth.UsernamePasswordCredentials;\nimportorg.apache.http.client.CredentialsProvider;\nimportorg.apache.http.client.methods.CloseableHttpResponse;\nimportorg.apache.http.client.methods.HttpGet;\nimportorg.apache.http.client.utils.URIBuilder;\nimportorg.apache.http.impl.client.BasicCredentialsProvider;\nimportorg.apache.http.impl.client.CloseableHttpClient;\nimportorg.apache.http.impl.client.HttpClients;\nimportjava.io.*;\nimportjava.net.URISyntaxException;\nimportjava.nio.file.CopyOption;\nimportjava.nio.file.Files;\nimportjava.nio.file.Paths;\nimportjava.nio.file.StandardCopyOption;\nimportjava.util.*;\n\n/**\n*Createdby01312170on14-12-24.\n*/\npublicclassHttpClientUtils{\n\n\tprivatestaticintBUF=4096;\n\tprivatestaticintFILE_BUF=1024*1024;\n\n\t/**\n\t*下载\n\t*@paramfileUrl文件HTTP地址\n\t*@paramlocalPath文件本地路径\n\t*@paramtempLocalPath文件本地临时路径\n\t*@paramisReload是否需要重新下载\n\t*@paramauthorUserName认证用户名\n\t*@paramauthorUserName认证密码\n\t*@return\n\t*@throwsIOException\n\t*@throwsURISyntaxException\n\t*/\n\tpublicstaticbooleandownloadFile(StringfileUrl,StringlocalPath,\n\t\tStringtempLocalPath,StringauthorUserName,StringauthorPassword,booleanisReload)throwsException{\n\t\tMap<String,Long>params=newHashMap<String,Long>();\n\t\tbooleanresult=true;\n\t\t//获取服务器上文件大小\n\t\tlongfileSize=getFileSize(fileUrl,authorUserName,authorPassword);\n\n\t\tparams.put(&34;,fileSize);\n\t\tparams.put(&34;,0L);\n\n\t\t//实际大小小于等于0,无需下载\n\t\tif(fileSize<=0){\n\t\t\t//returnparams;\n\t\t\treturnfalse;\n\t\t}\n\n\t\t//本地大小与实际大小不同,需要重新下载\n\t\tlonglocalSize=0;\n\t\tFilefile=newFile(localPath);\n\t\tif(!(file.getParentFile().exists()&&file.getParentFile().isDirectory())){\n\t\t\tfile.getParentFile().mkdirs();\n\t\t}\n\n\t\tFiletempFile=newFile(tempLocalPath);\n\t\tif(!(tempFile.getParentFile().exists()&&tempFile.getParentFile().isDirectory())){\n\t\t\ttempFile.getParentFile().mkdirs();\n\t\t}\n\n\t\tStringfileEtag=getFileEtag(fileUrl,authorUserName,authorPassword);\n\t\tStringetagFilePath=&34;;\n\t\tif(fileEtag!=null){\n\t\t\tetagFilePath=tempFile.getParentFile().getPath().concat(File.separator).concat(tempFile.getName()).concat(&34;);\n\t\t\tif(newFile(etagFilePath).exists()){\n\t\t\t\tStringoldFileEtag=readFileContent(etagFilePath);\n\t\t\t\tif(oldFileEtag==null){\n\t\t\t\t\tSystem.out.println(&34;);\n\t\t\t\t\treturnfalse;\n\t\t\t\t}\n\t\t\t\tif(!fileEtag.equals(oldFileEtag)){\n\t\t\t\t\tisReload=true;\n\t\t\t\t\t//写入新的etag\n\t\t\t\t\tif(!writeFileContent(etagFilePath,fileEtag)){\n\t\t\t\t\t\tSystem.out.println(&34;);\n\t\t\t\t\t\treturnfalse;\n\t\t\t\t\t};\n\t\t\t\t}else{\n\t\t\t\t\t//临时目录不存在,目标目录存在,且目标目录文件大小和线上大小相同则不需要下载,直接返回下载成功\n\t\t\t\t\tif(!tempFile.exists()&&file.exists()&&file.length()==fileSize){\n\t\t\t\t\t\treturntrue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tif(!writeFileContent(etagFilePath,fileEtag)){\n\t\t\t\t\tSystem.out.println(&34;);\n\t\t\t\t\treturnfalse;\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t//检测是否支持断点下载\n\t\tif(checkRange(fileUrl,authorUserName,authorPassword)){\n\t\t\tif(tempFile.exists()){\n\t\t\t\tif(tempFile.length()==fileSize){\n\t\t\t\t\tmoveFile(tempFile.getAbsolutePath(),file.getAbsolutePath());\n\t\t\t\t\treturntrue;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//支持分段\n\t\t\tRandomAccessFiletempLocalFile=newRandomAccessFile(tempLocalPath,&34;);\n\t\t\tlocalSize=tempLocalFile.length();\n\t\t\tif(localSize>fileSize||isReload){\n\t\t\t\tlocalSize=0;\n\t\t\t\ttempLocalFile.setLength(0);\n\t\t\t}\n\t\t\tCloseableHttpResponseresponse=null;\n\t\t\ttry{\n\t\t\t\tURIBuilderuriBuilder=newURIBuilder(fileUrl);\n\t\t\t\tCloseableHttpClienthttpClient;\n\t\t\t\tif(null!=authorUserName&&null!=authorPassword){\n\t\t\t\t\tCredentialsProviderprovider=newBasicCredentialsProvider();\n\t\t\t\t\tUsernamePasswordCredentialscredentials=newUsernamePasswordCredentials(authorUserName,authorPassword);\n\t\t\t\t\tprovider.setCredentials(AuthScope.ANY,credentials);\n\t\t\t\t\thttpClient=HttpClients.custom().setDefaultCredentialsProvider(provider).build();\n\t\t\t\t}else{\n\t\t\t\t\thttpClient=HttpClients.createDefault();\n\t\t\t\t}\n\t\t\t\tHttpGethttpGet=newHttpGet(uriBuilder.build());\n\t\t\t\thttpGet.addHeader(&34;,&34;+localSize+&34;);\n\t\t\t\tresponse=httpClient.execute(httpGet);\n\t\t\t\tHttpEntityentity=response.getEntity();\n\t\t\t\tif(entity!=null){\n\t\t\t\t\tInputStreaminstream=entity.getContent();\n\t\t\t\t\ttry{\n\t\t\t\t\t\tbyte[]_r=newbyte[FILE_BUF];\n\t\t\t\t\t\tintlength;\n\t\t\t\t\t\twhile((length=instream.read(_r))!=-1){\n\t\t\t\t\t\t\ttempLocalFile.seek(localSize);\n\t\t\t\t\t\t\ttempLocalFile.write(_r,0,length);\n\t\t\t\t\t\t\tlocalSize=localSize+length;\n\t\t\t\t\t\t}\n\t\t\t\t\t}catch(Exceptione){\n\t\t\t\t\t\tresult=false;\n\t\t\t\t\t\te.printStackTrace();\n\t\t\t\t\t\tthrownewException();\n\t\t\t\t\t}finally{\n\t\t\t\t\t\tinstream.close();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}catch(Exceptione){\n\t\t\t\tresult=false;\n\t\t\t\te.printStackTrace();\n\t\t\t\tthrownewException();\n\t\t\t}finally{\n\t\t\t\tif(response!=null){\n\t\t\t\t\tresponse.close();\n\t\t\t\t}\n\t\t\t\tif(tempLocalFile!=null){\n\t\t\t\t\ttempLocalFile.close();\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\t//不支持分段\n\t\t\tRandomAccessFiletempLocalFile=newRandomAccessFile(tempLocalPath,&34;);\n\t\t\tlocalSize=0;\n\t\t\tCloseableHttpResponseresponse=null;\n\t\t\ttry{\n\t\t\t\tURIBuilderuriBuilder=newURIBuilder(fileUrl);\n\t\t\t\tCloseableHttpClienthttpClient;\n\t\t\t\tif(null!=authorUserName&&null!=authorPassword){\n\t\t\t\t\tCredentialsProviderprovider=newBasicCredentialsProvider();\n\t\t\t\t\tUsernamePasswordCredentialscredentials=newUsernamePasswordCredentials(authorUserName,authorPassword);\n\t\t\t\t\tprovider.setCredentials(AuthScope.ANY,credentials);\n\t\t\t\t\thttpClient=HttpClients.custom().setDefaultCredentialsProvider(provider).build();\n\t\t\t\t}else{\n\t\t\t\t\thttpClient=HttpClients.createDefault();\n\t\t\t\t}\n\t\t\t\tHttpGethttpGet=newHttpGet(uriBuilder.build());\n\t\t\t\tresponse=httpClient.execute(httpGet);\n\t\t\t\tHttpEntityentity=response.getEntity();\n\t\t\t\tif(entity!=null){\n\t\t\t\t\tInputStreaminstream=entity.getContent();\n\t\t\t\t\ttry{\n\t\t\t\t\t\tbyte[]_r=newbyte[FILE_BUF];\n\t\t\t\t\t\tintlength;\n\t\t\t\t\t\twhile((length=instream.read(_r))!=-1){\n\t\t\t\t\t\t\ttempLocalFile.seek(localSize);\n\t\t\t\t\t\t\ttempLocalFile.write(_r,0,length);\n\t\t\t\t\t\t\tlocalSize=localSize+length;\n\t\t\t\t\t\t}\n\t\t\t\t\t}catch(Exceptione){\n\t\t\t\t\t\tresult=false;\n\t\t\t\t\t\te.printStackTrace();\n\t\t\t\t\t\tthrownewException();\n\t\t\t\t\t}finally{\n\t\t\t\t\t\tif(instream!=null){\n\t\t\t\t\t\t\tinstream.close();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}catch(Exceptione){\n\t\t\t\te.printStackTrace();\n\t\t\t\tresult=false;\n\t\t\t\tthrownewException();\n\t\t\t}finally{\n\t\t\t\tif(response!=null){\n\t\t\t\t\tresponse.close();\n\t\t\t\t}\n\t\t\t\tif(tempLocalFile!=null){\n\t\t\t\t\ttempLocalFile.close();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(localSize==fileSize){\n\t\t\ttry{\n\t\t\t\tmoveFile(tempLocalPath,localPath);\n\t\t\t}catch(Exceptione){\n\t\t\t\tresult=false;\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}else{\n\t\t\tresult=false;\n\t\t}\n\t\tparams.put(&34;,localSize);\n\t\t//下载完成,并且在linux系统下,更改权限\n\t\treturnresult;\n\n\t}\n\n\tpublicstaticvoidmoveFile(Stringsource,Stringtarget){\n\t\ttry{\n\t\t\tFiles.move(Paths.get(source),Paths.get(target),StandardCopyOption.REPLACE_EXISTING);\n\t\t\tSystem.out.println(&34;);\n\t\t}catch(Exceptione){\n\t\t\tSystem.out.println(&34;);\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n\n\tpublicstaticlonggetFileSize(Stringurl,StringauthorUserName,StringauthorPassword){\n\t\tCloseableHttpClienthttpClient;\n\t\tLongsize=-2L;\n\t\ttry{\n\t\t\tif(null!=authorUserName&&null!=authorPassword){\n\t\t\t\tCredentialsProviderprovider=newBasicCredentialsProvider();\n\t\t\t\tUsernamePasswordCredentialscredentials=newUsernamePasswordCredentials(authorUserName,authorPassword);\n\t\t\t\tprovider.setCredentials(AuthScope.ANY,credentials);\n\t\t\t\thttpClient=HttpClients.custom().setDefaultCredentialsProvider(provider).build();\n\t\t\t}else{\n\t\t\t\thttpClient=HttpClients.createDefault();\n\t\t\t}\n\t\t\tHttpGethttpget=newHttpGet(url);\n\t\t\tHeaderheader;\n\t\t\tCloseableHttpResponsecloseableHttpResponse=httpClient.execute(httpget);\n\t\t\tintstatusCode=closeableHttpResponse.getStatusLine().getStatusCode();\n\t\t\tif(statusCode>400){\n\t\t\t\treturnsize;\n\t\t\t}\n\t\t\theader=closeableHttpResponse.getFirstHeader(&34;);\n\t\t\tif(header!=null){\n\t\t\t\tif(null!=header.getValue()){\n\t\t\t\t\tsize=Long.parseLong(header.getValue());\n\t\t\t\t}\n\t\t\t}\n\t\t\tcloseableHttpResponse.close();\n\t\t\thttpClient.close();\n\t\t}catch(Exceptione){\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturnsize;\n\t}\n\n\tpublicstaticStringgetFileEtag(Stringurl,StringauthorUserName,StringauthorPassword){\n\t\tCloseableHttpClienthttpClient;\n\t\tStringeTag=&34;;\n\t\ttry{\n\t\t\tif(null!=authorUserName&&null!=authorPassword){\n\t\t\t\tCredentialsProviderprovider=newBasicCredentialsProvider();\n\t\t\t\tUsernamePasswordCredentialscredentials=newUsernamePasswordCredentials(authorUserName,authorPassword);\n\t\t\t\tprovider.setCredentials(AuthScope.ANY,credentials);\n\t\t\t\thttpClient=HttpClients.custom().setDefaultCredentialsProvider(provider).build();\n\t\t\t}else{\n\t\t\t\thttpClient=HttpClients.createDefault();\n\t\t\t}\n\t\t\tHttpGethttpget=newHttpGet(url);\n\t\t\tHeaderheader;\n\t\t\tCloseableHttpResponsecloseableHttpResponse=httpClient.execute(httpget);\n\t\t\tintstatusCode=closeableHttpResponse.getStatusLine().getStatusCode();\n\t\t\tif(statusCode>400){\n\t\t\t\treturneTag;\n\t\t\t}\n\t\t\theader=closeableHttpResponse.getFirstHeader(&34;);\n\t\t\tif(header!=null){\n\t\t\t\tif(null!=header.getValue()){\n\t\t\t\t\teTag=header.getValue().replace(&34;&34;&34;ETag&34;\\&34;,&34;);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcloseableHttpResponse.close();\n\t\t\thttpClient.close();\n\t\t}catch(Exceptione){\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturneTag;\n\t}\n\n\tpublicstaticbooleancheckRange(StringfileUrl,StringauthorUserName,StringauthorPassword)throwsIOException{\n\t\tCloseableHttpClienthttpClient;\n\t\tif(null!=authorUserName&&null!=authorPassword){\n\t\t\tCredentialsProviderprovider=newBasicCredentialsProvider();\n\t\t\tUsernamePasswordCredentialscredentials=newUsernamePasswordCredentials(authorUserName,authorPassword);\n\t\t\tprovider.setCredentials(AuthScope.ANY,credentials);\n\t\t\thttpClient=HttpClients.custom().setDefaultCredentialsProvider(provider).build();\n\t\t}else{\n\t\t\thttpClient=HttpClients.createDefault();\n\t\t}\n\t\tHttpGethttpget=newHttpGet(fileUrl);\n\t\thttpget.addHeader(&34;,&34;);\n\t\tCloseableHttpResponsecloseableHttpResponse=httpClient.execute(httpget);\n\t\tHeaderheader=closeableHttpResponse.getFirstHeader(&34;);\n\t\tif(header!=null&&header.getValue().length()>&34;.length()){\n\t\t\tcloseableHttpResponse.close();\n\t\t\thttpClient.close();\n\t\t\treturntrue;\n\t\t}\n\t\tcloseableHttpResponse.close();\n\t\thttpClient.close();\n\t\treturnfalse;\n\t}\n\n\tpublicstaticStringreadFileContent(Stringpath)throwsIOException{\n\t\tFileInputStreamfis=null;\n\t\tInputStreamReaderisr=null;\n\t\tBufferedReaderbr=null;\n\t\ttry{\n\t\t\tfis=newFileInputStream(path);\n\t\t\tisr=newInputStreamReader(fis,&34;);\n\t\t\tbr=newBufferedReader(isr);\n\t\t\treturnbr.readLine();\n\t\t}catch(FileNotFoundExceptione){\n\t\t\te.printStackTrace();\n\t\t}catch(UnsupportedEncodingExceptione){\n\t\t\te.printStackTrace();\n\t\t}catch(IOExceptione){\n\t\t\te.printStackTrace();\n\t\t}finally{\n\t\t\tif(br!=null){\n\t\t\t\tbr.close();\n\t\t\t}\n\t\t\tif(isr!=null){\n\t\t\t\tisr.close();\n\t\t\t}\n\t\t\tif(fis!=null){\n\t\t\t\tfis.close();\n\t\t\t}\n\t\t}\n\t\treturnnull;\n\t}\n\n\tpublicstaticBooleanwriteFileContent(Stringpath,Stringcontent){\n\t\tBooleanresult=true;\n\t\tFileOutputStreamfos=null;\n\t\tOutputStreamWriterosw=null;\n\t\ttry{\n\t\t\tfos=newFileOutputStream(path);\n\t\t\tosw=newOutputStreamWriter(fos,&34;);\n\t\t\tosw.write(content);\n\t\t\tosw.flush();\n\t\t}catch(FileNotFoundExceptione){\n\t\t\tresult=false;\n\t\t\te.printStackTrace();\n\t\t}catch(UnsupportedEncodingExceptione){\n\t\t\tresult=false;\n\t\t\te.printStackTrace();\n\t\t}catch(IOExceptione){\n\t\t\tresult=false;\n\t\t\te.printStackTrace();\n\t\t}finally{\n\t\t\ttry{\n\t\t\t\tif(osw!=null){\n\t\t\t\t\tosw.close();\n\t\t\t\t}\n\t\t\t\tif(fos!=null){\n\t\t\t\t\tfos.close();\n\t\t\t\t}\n\t\t\t}catch(IOExceptione){\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t\treturnresult;\n\t}\n\npublicstaticvoidmain(String[]args){\nStringfileUrl=&34;;\nStringlocalPath=&34;;\nStringtempLocalPath=&34;;\nStringauthorUserName=null;\nStringauthorPassword=null;\nbooleanisReload=false;\ntry{\ndownloadFile(fileUrl,localPath,tempLocalPath,authorUserName,authorPassword,isReload);\n}catch(Exceptione){\ne.printStackTrace();\n}\n}\n\n}
关于验证网站源码分享和检测网站源码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
