📄 fileutil.java
字号:
package cn.myvideosite.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import cn.myvideosite.exception.DownloadException;
import cn.myvideosite.util.MyRandom;;
public class FileUtil {
private static final String ROOT="d:\\image2\\";
private static final int timeout=88888;
/**
* 根据系统当前时间,构造目录结构
* java.util.Date, Canlender
* java.io.File http://i3.sinaimg.cn/jc/bbs/p(/2008/0912/)+U1335P27T90D28792F1561DT+20080912140030.jpg
*/
private static String mkdirs(){
Calendar cal=Calendar.getInstance();
StringBuffer sb=new StringBuffer();
sb.append(cal.get(Calendar.YEAR)).append(File.separator).append(cal.get(Calendar.MONTH)+1)
.append(cal.get(Calendar.DAY_OF_MONTH)).append(File.separator).append(cal.get(Calendar.HOUR_OF_DAY))
.append(cal.get(Calendar.MINUTE)).append(File.separator).toString();
File f=new File(ROOT+sb.toString());
if(!f.exists())
f.mkdirs();
return sb.toString();
}
/**
*
* @return 依据当前系统时间加上随即数
* 得到新文件名
*/
private static String getNewFileName(String fileUrl){
Calendar cal=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
String strDate=sdf.format(cal.getTime());
String fileFormat=fileUrl.substring(fileUrl.lastIndexOf("."));
//System.out.println(mkdirs()+MyRandom.random()+strDate+fileFormat);
return MyRandom.random()+strDate+fileFormat;
}
public static String download(String fileUrl)throws DownloadException{
String path = mkdirs()+getNewFileName(fileUrl);
String fileName=ROOT+path;
download(fileUrl,fileName);
return path.replace("\\", "/");
}
/**
* @param args 文件下载工具类
*/
public static void download(String fileUrl,String savePath)throws DownloadException{
BufferedOutputStream bos=null;
InputStream is=null;
HttpURLConnection urlconnection=null;
try {
URL url=new URL(fileUrl);
urlconnection=(HttpURLConnection)url.openConnection();
if(null!=urlconnection){
urlconnection.setConnectTimeout(timeout);
urlconnection.setReadTimeout(timeout);
urlconnection.connect();
int i=urlconnection.getResponseCode();// 从 HTTP 响应消息获取状态码。
if(i!=200) throw new DownloadException("连接到:"+fileUrl+"失败,返回CODE:"+i);
int contentLength=urlconnection.getContentLength();//取得连接内容的长度
is=urlconnection.getInputStream();//得到服务器返回的InputStream
File outFile=new File(savePath);//写文件
int readTotalLength=0;
byte[] barr= new byte[1024];
bos=new BufferedOutputStream(new FileOutputStream(outFile));
int readLength=0;
while((readLength=is.read(barr))!=-1){
bos.write(barr,0,readLength);
readTotalLength+=readLength;
}
if(readTotalLength<contentLength){ //校检是否下载正常
bos.flush();
is.close();
bos.close();
outFile.delete();
throw new DownloadException("下载失败!!"+fileUrl);
}
bos.flush();
}
} catch (IOException e) {
new DownloadException(e);
}finally{
try {
if(is!=null){
is.close();
is=null;
}
if(bos!=null){
bos.close();
bos=null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
// d:/image/2008/927/30/r4IJMA30TcX6pkv20080927030002.jpg
//d:/image/2008/927/259/phvG5HkOcr0A22j20080927025944.jpg
//d:/image/2008/927/31/6uGM5qhaggPwSea20080927030116.jpg
try {
download("D:\\image\\2008\\927\\9386uGM5qhaggPwSea20080927030116.jpg");
} catch (DownloadException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -