我在这里记录一下好用的工具类
首先是url—>inputStream
public static InputStream downLoadFromUrl(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
return conn.getInputStream();
}
然后是InputStream–>byte[]
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
byte[]---->file
public static void saveFile(byte [] data,String filename)throws Exception{
if(data != null){
String filepath ="D:\\"+filename ;
File file = new File(filepath);
if(file.exists()){
file.delete();
file.createNewFile();
}else {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(data,0,data.length);
fos.flush();
fos.close();
}
}
之前还是太嫩,比加班更加想离职的就是出差加班,比出差加更过分的就是出差不让人吃饱而且吃的还不能够报销,就这样吧,毁灭吧