我使用以下代码调整图像大小并将其存储在临时文件中
public File resizeImage(InputStream fileInputStream, String fileName, int newW,int newH) throws Exception {
Graphics2D g = null;
File file2= File.createTempFile("result",FilenameUtils.getExtension(fileName));
BufferedImage img = null;
File tempFile1 = File.createTempFile("Temp1File",FilenameUtils.getExtension(fileName)); // If not resizing send temp file1
FileOutputStream outputStream = null;
try{
outputStream = new FileOutputStream(tempFile1);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
outputStream = null;
img = ImageIO.read(new FileInputStream(tempFile1));
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
ImageIO.write(dimg, FilenameUtils.getExtension(fileName), file2);
} catch(SocketException e) {
e.printStackTrace();
throw e;
} catch(Exception e) {
e.printStackTrace();
throw e;
} finally {
if(outputStream != null)
outputStream.close();
if(fileInputStream != null)
fileInputStream.close();
if(g != null)
g.dispose();
if(img != null)
img.flush();
}
return file2;
}
当我在突出显示的行中读取tempFile1时,我会遇到异常 套接字例外:套接字已经关闭。 任何人都可以帮助我