public static void Dofiledownload (String path,
HttpServletResponse response, String filename, int width, int hight)
Throws Exception {
Path refers to the paths of the files you want to download.
File File = new file (path);
Image img = imageio.read (file); Constructing an Image Object
int srcwidth = Img.getwidth (null); Get the source map width
int srcheight = Img.getheight (null); Get the source graph length
Compression width or height max width hight max height
if (Srcwidth/srcheight > Width/hight) {
int h = (int) (srcheight * width/srcwidth);
Resize (img, width, h, response, filename, file);
} else {
int w = (int) (srcwidth * hight/srcheight);
Resize (IMG, W, hight, response, filename, file);
}
}
/**
* Force compress/Enlarge picture to fixed size
*
* @param W
* Int new width
* @param h
* Int new Height
*/
public static void Resize (Image img, int w, int h,
HttpServletResponse response, String filename, file file)
Throws IOException {
Scale_smooth's thumbnail algorithm produces thumbnail images with higher priority than high-speed image quality but slower
BufferedImage image = New BufferedImage (W, H,
BUFFEREDIMAGE.TYPE_INT_RGB);
Image.getgraphics (). DrawImage (img, 0, 0, W, h, NULL); Draw a zoomed-out diagram
String NewFileName = getnewfilename () + ". jpg";
File DestFile = new file (ImagePath + file.separator + newfilename);
FileOutputStream out = new FileOutputStream (destfile); Output to file stream
BMP, PNG, GIF to JPG can be implemented normally
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image); JPEG encoding
Out.close ();
Empty response
Response.reset ();
Set the header of the response
Response.AddHeader ("Content-disposition", "Attachment;filename="
+ New String (Filename.getbytes ()));
Response.AddHeader ("Content-length", "" "+ Destfile.length ());
Response.setcontenttype ("Application/octet-stream");
BMP, PNG, GIF to JPG can be implemented normally
OutputStream toclient = new Bufferedoutputstream (
Response.getoutputstream ());
Download the file in the form of a stream.
InputStream fis = new Bufferedinputstream (new FileInputStream (DestFile));
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Fis.close ();
Toclient.write (buffer);
Toclient.flush ();
Toclient.close ();
}
Download Compressed pictures