这次这个是复制图片到规定路径,其实就是读出来选定的图片,然后在规定的路径下写入。
上代码:
上代码:
private void saveImage(String path,String savePath) throws Exception {
// path是源图片的路径 完整的 如: c:/temp/123.jpg
//savePath是目标路径 完整的 如: d:/temp/456.jpg
File file = new File (savePath.substring(0,savePath.lenth()-11));
//这里的savePath.substring(0,savePath.lenth()-11是需要用的时候改动的,因为这里是创建目录的,不要 图片名字
if(null != file && !file.isDirectory()){
file.mkdirs();
}
//写入图片
BufferedInputStream ips = new BufferedInputStream(new URL(path).openStream());
File saveFile = new File(savePath);
BufferedOutputStream ops = new BufferedOutputStream(new FileOutputStream(saveFile));
byte[] buf = new byte[1024];
int i =0;
boolean isFirst = true;
try{
while((i=ips.read(buf)) != -1){
ops.write(buf,0,i);
}
}catch(Exception e){
throw e;
}finally{
ops.flush();
if(ips != null)ips.close();
if(ops != null) ops.close();
}
}