📄 filemanager.java
字号:
package com.hssj.gimts.client.advertisement;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
/**
* Description:用http从服务器下载文件
* Date 2007-11-12
* @version 1.0
*/
public abstract class FileManager {
/**
* Description: 下载文件的长度
*/
private int fileLength;
/**
* Description: 缓冲字节数
*/
private final static int READFILESIZE = 8192;
/**
* Description:获取服务器的http输入流
* @param fileurl:服务器文件的url地址
* @return 返回服务器的http输入流
*/
public InputStream createInputStream(String fileurl) {
InputStream input = null;
URLConnection conn;
int httpStatusCode;
try {
URL url = new URL(fileurl);
conn = url.openConnection();
conn.connect();
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpStatusCode = httpconn.getResponseCode();
if (httpStatusCode != HttpURLConnection.HTTP_OK) {// HttpURLConnection
System.out.println("Connect to " + fileurl
+ " failed,return code:" + httpStatusCode);
return null;
}
fileLength = conn.getContentLength();
input = conn.getInputStream();
} catch (Exception e) {
return null;
//e.printStackTrace();
}
return input;
};
/**
* Description:获取服务器文件长度
* @param fileurl:服务器文件的url地址
* @return 返回服务器文件长度
*/
public int connectionFileLength(String fileurl) {
URLConnection conn;
int httpStatusCode;
try {
URL url = new URL(fileurl);
conn = url.openConnection();
conn.connect();
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpStatusCode = httpconn.getResponseCode();
if (httpStatusCode != HttpURLConnection.HTTP_OK) {// HttpURLConnection
System.out.println("Connect to " + fileurl
+ " failed,return code:" + httpStatusCode);
return -1;
} else
return conn.getContentLength();
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/**
* Description:从服务器下载文件到本地
* @param fileurl:服务文件url
* @param saveFile:本地文件
* @return 如果下载成功返回true,下载失败返回false
*/
public boolean WriteToSaveFile(String fileurl, String saveFile) {
InputStream in = createInputStream(fileurl);
if (in != null) {
File savefile = new File(saveFile);
try {
if (!savefile.exists())
savefile.createNewFile();
FileOutputStream fos = new FileOutputStream(saveFile);
return WriteToSaveFile(in, fos);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else
return false;
};
/**
* Description:从输入流读取数据写入输出流
* @param input:数据源(输入流)
* @param out:输出流
* @return 写完成返回true,有异常返回false
* @throws IOException
*/
public boolean WriteToSaveFile(InputStream input, OutputStream out)
throws IOException {
int blockSize = READFILESIZE;
int readBytes = 0;
byte[] tmpbuf = new byte[READFILESIZE];
int totalRead = 0;
while ((readBytes = input.read(tmpbuf, 0, blockSize)) != -1) {
totalRead += readBytes;
out.write(tmpbuf, 0, readBytes);
}
out.flush();
input.close();
out.close();
if (totalRead < getFileLength())// 未下载完成
return false;
else
return true;
}
/**
* Description:从属性文件中读取数字写入输出流
* @param input:属性文件流
* @param out:输出流
*/
public void WriteToSaveFile(Properties input, OutputStream out) {
try {
input.store(out, " ");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Description:获取文件长度
* @return 返回文件长度
*/
public synchronized int getFileLength() {
return this.fileLength;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -