总会有这样的需求,让你去下载某个固定服务器上的文件信息,那么就会用到文件下载的应用。
贴点有用的干货。。。
/**
* 文件下载方法p
* @param fUrl 源文件地址 如:http://localhost/ExportData/fileDownLoadsServlet?fileName=demo.txt
* @param sUrl 保存地址 如:D://
* @return
*/
public boolean DownFile(String fUrl,String sUrl){
String[] urlname = fUrl.split("=");
int len = urlname.length-1;
String uname = urlname[len];//获取文件名
try {
File file = new File(sUrl+uname);//创建新文件
if(file!=null && !file.exists()){
file.createNewFile();
}
OutputStream oputstream = new FileOutputStream(file);
URL url = new URL(fUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:"+uc.getContentLength());//打印文件长度
byte[] buffer = new byte[4*1024];
int byteRead = -1;
while((byteRead=(iputstream.read(buffer)))!= -1){
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
System.out.println("源文件:"+fUrl);
System.out.println("下载到:"+sUrl+uname);
return true;
} catch (Exception e) {
System.out.println("下载失败!");
e.printStackTrace();
return false;
}
}
//下载并输出内容。
public static String DownAndReadFile(String filePath) {
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File savePath = new File("D://" + date);// 创建新文件
if (!savePath.exists()) {
savePath.mkdir();
}
String[] urlname = filePath.split("=");
int len = urlname.length - 1;
String uname = urlname[len];// 获取文件名
try {
File file = new File(savePath + "//" + uname);// 创建新文件
if (file != null && !file.exists()) {
file.createNewFile();
}
OutputStream oputstream = new FileOutputStream(file);
URL url = new URL(filePath);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);// 设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:" + uc.getContentLength());// 打印文件长度
byte[] buffer = new byte[4 * 1024];
int byteRead = -1;
while ((byteRead = (iputstream.read(buffer))) != -1) {
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
// 读取文件
StringBuffer strb = new StringBuffer();
FileInputStream fs = new FileInputStream(new File(savePath + "//"+ uname));
InputStreamReader isr = new InputStreamReader(fs, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String data = "";
while ((data = br.readLine()) != null) {
strb.append(data + "\n");
}
br.close();
fs.close();
isr.close();
System.out.println(strb.toString());
return strb.toString();
} catch (Exception e) {
System.out.println("读取失败!");
e.printStackTrace();
}
return null;
}
private static void createFile(String fromUrl) {
try {
String fileName = fromUrl.split("=")[1];
//fileName= "/home/kaifa/"+fileName;
fileName= "d:\\"+fileName;
File file = new File(fileName);
URL url = new URL(fromUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoInput(true);// 设置是否要从 URL 连接读取数据,默认为true
httpConn.connect();
//read
InputStream input = httpConn.getInputStream();
InputStreamReader isr = new InputStreamReader(input,"GBK");
BufferedReader reader = new BufferedReader(isr);
//write
OutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out,"GBK");
BufferedWriter writer = new BufferedWriter(osw);
String line = null;
while ((line=reader.readLine())!=null) {
writer.write(line+"\n");
}
writer.close();
osw.close();
out.close();
reader.close();
isr.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
实现有很多,但是大多数都是使用url ,httpurlconnection 来实现的。。。