private boolean downloadFileString httpUrl, String savePath) { int byteread = 0; try { URL url = new URLhttpUrl); URLConnection conn = url.openConnection); InputStream inStream = conn.getInputStream); FileOutputStream fs = new FileOutputStreamsavePath); byte[] buffer = new byte[1204]; while byteread = inStream.readbuffer)) != -1) { fs.writebuffer, 0, byteread); } System.out.printlnsavePath+" download finished!"); return true; } catch MalformedURLException e) { e.printStackTrace); return false; } catch IOException e) { e.printStackTrace); return false; } }
上传文件保存在服务器:
private String saveFileMultipartFile file) {
try {
iffile != null && !file.isEmpty)) {
String filePath = "保存至服务器的地址"
File fp = new Filenew FilefilePath).getParent));
if!fp.exists)){
fp.mkdirs);
}
DataOutputStream out = new DataOutputStreamnew FileOutputStreamfilePath));
InputStream is = null;
try {
is = file.getInputStream);
byte[] b=new byte[is.available)];
is.readb);
out.writeb);
return filePath ;
} catch Exception e) {
throw new RuntimeExceptione);
} finally {
if is != null) {
is.close);
}
if out != null) {
out.close);
}
}
}
} catch Exception e) {
throw new RuntimeExceptione);
}
return null;
}
