Download Compressed pictures

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.