importjava.net.*;
importjava.io.*;
publicclassURLConnectionDemo{
publicstaticvoidmain(String[]args)throwsException{
URLurl=newURL("http://www.scp.e.cn/pantoschoolzz/BG/Bord/Message/DownloadMessageAttachment.aspx?ID=215");
URLConnectionuc=url.openConnection();
StringfileName=uc.getHeaderField(6);
fileName=URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
System.out.println("文件名為:"+fileName);
System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
Stringpath="D:"+File.separator+fileName;
FileOutputStreamos=newFileOutputStream(path);
InputStreamis=uc.getInputStream();
byte[]b=newbyte[1024];
intlen=0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
System.out.println("下載成功,文件保存在:"+path);
}
}
//給你一個下載的例子吧,僅供參考。
B. java如何實現超鏈接下載
java實現超鏈接下載方法如下:
response.setHeader("Content-disposition","attachment;filename="下載的文件名字);
備註:讓response調用setheader方法添加下載的頭給客戶的瀏覽器,瀏覽器收到該頭後就會打開相應的下載對話框。
C. java程序下載pdf文件
主要是 URL 和 HttpURLConnection 類的運用,看代碼:
importjava.io.DataInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;
publicclassHttpDownloader{
_FILE_URL="http://211.103.156.163/u/cms/www/201511/25051940i6ou.pdf";
privatestaticfinalStringLOCAL_FILE_PATH="D:/some.pdf";//改成你保存文件的路徑
publicstaticvoidmain(String[]args){
(REMOTE_FILE_URL,LOCAL_FILE_PATH).download();
}
privateStringremoteFileUrl;
privateStringlocalFilePath;
publicHttpDownloader(StringremoteFileUrl,StringlocalFilePath){
this.remoteFileUrl=remoteFileUrl;
this.localFilePath=localFilePath;
}
publicvoiddownload(){
try{
URLurl=newURL(remoteFileUrl);
=(HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(5*1000);//5000毫秒內沒有連接上則放棄連接
httpURLConnection.connect();//連接
System.out.println("連接URL成功~");
intfileLenght=httpURLConnection.getContentLength();
System.out.println("文件大小:"+(fileLenght/1024.0)+"KB");
System.out.println("開始下載...");
try(DataInputStreamdis=newDataInputStream(httpURLConnection.getInputStream());
FileOutputStreamfos=newFileOutputStream(localFilePath)){
byte[]buf=newbyte[10240];//根據實際情況可以增大buf大小
for(intreadSize;(readSize=dis.read(buf))>0;){
fos.write(buf,0,readSize);
}
System.out.println("下載完畢~");
}catch(IOExceptionex){
System.out.println("下載時出錯");
}
httpURLConnection.disconnect();
}catch(IOExceptionex){
System.out.println("URL不存在或者連接超時");
}
}
}
D. java怎樣讀取http文件伺服器上的文件列表並下載
要求文件名不能寫死,那麼只能到伺服器上去遍歷目錄,如果伺服器開了ftp許可權的話到可以用apache的commons-net包,裡面有ftp功能可以上傳下載文件,也可以遍歷文件
E. 如何用java語言直接從web上下載數據,從而省去在網頁上手動點擊下載
URL url = new URL("http://219.219.114.10/infobin/select.dll");
URLConnection uc = url.openConnection();
InputStreamReader is = new InputStreamReader(uc.getInputStream());
int line;
StringBuffer sb = new StringBuffer("");
while((line=is.read())!=-1){
sb.append((char)line);
}
String str = sb.toString();
//解析其中的內容
//可以通過 找到有用的地址文件,然後利用如下
url = new URL(「有用的文件路徑」);
BufferedInputStream in = new BufferedInputStream(url.openStream());
哈哈,得到它了,一切就ok啦
下面的會了嗎,流操作,寫到本地
F. java 如何下載文件
httpURLConnection conn;
conn.getInputStream;
再將這個stream 寫到文件就可以了
G. Java獲取下載文件的大小
可以直接通過HttpURLConnection 的getContentLength()方法來獲取下載文件的大小。
//找到要下載的文件
url=new URL( "http://www..com/uploadfile/oracle.txt");
//根據響版應獲取文件大權小
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//獲取相應的文件長度
fileLength=urlcon.getContentLength();
備註:以上方法中url變換即可,下面的方法不用變更,即可獲取到對應下載文件的大小。
H. Java 下載文件的方法怎麼寫
參考下面
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下載的文件的路徑。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的後綴名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下載文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
// 下載本地文件
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
String fileName = "Operator.doc".toString(); // 文件的默認保存名
// 讀到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
// 設置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循環取出流中的數據
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 下載網路文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//支持在線打開文件的一種方式
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在線打開方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名應該編碼成UTF-8
} else { // 純下載方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
I. java下載伺服器上的文件到客戶端
java編程方法下載伺服器上的文件到本地客服端,代碼如下:
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;
publicclassDownLoad{
publicstaticvoiddownloadFile(URLtheURL,StringfilePath)throwsIOException{
FiledirFile=newFile(filePath);
if(!.exists()){
//文件路徑不存在時,自動創建目錄
dirFile.mkdir();
}
//從伺服器上獲取圖片並保存
URLConnectionconnection=theURL.openConnection();
InputStreamin=connection.getInputStream();
FileOutputStreamos=newFileOutputStream(filePath+"\123.png");
byte[]buffer=newbyte[4*1024];
intread;
while((read=in.read(buffer))>0){
os.write(buffer,0,read);
}
os.close();
in.close();
}
publicstaticvoidmain(String[]args){
//下面添加伺服器的IP地址和埠,以及要下載的文件路徑
StringurlPath="http://伺服器IP地址:埠/image/123.png";
//下面代碼是下載到本地的位置
StringfilePath="d:\excel";
URLurl=newURL(urlPath);
try{
downloadFile(url,filePath);
}catch(IOExceptione){
e.printStackTrace();
}
}
}