求一个java图片上传代码,不在jsp中实现,jsp中只是获得图片的路径,其他操作全在类中操作,求详细代码 200
1个回答
展开全部
public boolean copyPic(File file, String path) {
FileInputStream fi = null;
BufferedInputStream in = null;
FileOutputStream fo = null;
BufferedOutputStream out = null;
boolean bb = true;
try {
fi = new FileInputStream(file);// 原图
in = new BufferedInputStream(fi);// 读入缓存
File desFile = new File(path);
fo = new FileOutputStream(desFile, false);// 新图,如果是true就不会覆盖原图,如果false就覆盖
out = new BufferedOutputStream(fo);
byte[] buf = new byte[1024];
int len = in.read(buf);// 读文件,将读到的内容放入到buf数组中,返回的是读到的长度
while (len != -1) {
out.write(buf, 0, len);
len = in.read(buf);
}
} catch (Exception e) {
bb = false;
System.out.println("copy出错1...");
} finally {
try {
out.close();
fo.close();
in.close();
fi.close();
System.out.println("复制图片成功!");
} catch (IOException e) {
bb = false;
System.out.println("关闭图片出错2...");
}
}
return bb;
}
你很幸运,我刚刚写过的
FileInputStream fi = null;
BufferedInputStream in = null;
FileOutputStream fo = null;
BufferedOutputStream out = null;
boolean bb = true;
try {
fi = new FileInputStream(file);// 原图
in = new BufferedInputStream(fi);// 读入缓存
File desFile = new File(path);
fo = new FileOutputStream(desFile, false);// 新图,如果是true就不会覆盖原图,如果false就覆盖
out = new BufferedOutputStream(fo);
byte[] buf = new byte[1024];
int len = in.read(buf);// 读文件,将读到的内容放入到buf数组中,返回的是读到的长度
while (len != -1) {
out.write(buf, 0, len);
len = in.read(buf);
}
} catch (Exception e) {
bb = false;
System.out.println("copy出错1...");
} finally {
try {
out.close();
fo.close();
in.close();
fi.close();
System.out.println("复制图片成功!");
} catch (IOException e) {
bb = false;
System.out.println("关闭图片出错2...");
}
}
return bb;
}
你很幸运,我刚刚写过的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询