用java写一个实现图片上传下载功能的代码
时间: 2023-03-12 17:55:15 浏览: 138
可以使用Java IO流来实现图片上传和下载的功能,可以参考下面的代码:// 上传图片
FileInputStream fis = new FileInputStream(new File("image.png"));
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
fis.close();
bis.close();
os.close();// 下载图片
InputStream is = new FileInputStream(new File("image.png"));
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = response.getOutputStream();
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
is.close();
bis.close();
os.close();
阅读全文