Java compressed files and download pictures sample _java

Source: Internet
Author: User

This example for you to share the Java compressed files and download pictures samples for your reference, the specific contents are as follows

Main Page Index.xml

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>

 
 

Compressed file: Gzipservlet.java
Only compressed files are large enough to offset the compression overhead and effectively compress. Otherwise, the file size becomes larger after the small file is compressed

Package cn.hncu.servlet;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;

Import Java.util.zip.GZIPOutputStream;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; public class Gzipservlet extends HttpServlet {@Override protected void service (HttpServletRequest req, httpservletre Sponse resp) throws Servletexception, IOException {String str= "vuttyjhgyurc Hunan Changsha surdivsf Anhui Wuhu 890-80897 Maybe longer v will get used to it. Tvsduvgkjxhvnxzc.nlkcjsdfpeifniuq4ey8f048eyuyli "+"
Skchkxhckxzncnxclkjhasliduhasiduisugdlisgdlkjadhlksjhdlkasjdhasklhdlkjsahashdkskdjhkdjshkldsjhlksjhfkljd ";

    Byte src[]=str.getbytes (),//using Platform default encoding (GBK), no use of iso-8859-1 byte src[]=str.getbytes ("Utf-8"); Background to the browser sent data, browsers do not know-need to download, with text to open is garbled. So to set the protocol Bytearrayoutputstream bout=new Bytearrayoutputstream ();//Memory stream Gzipoutputstream gout=new gzipoutputstReam (BOut); Gout.write (SRC);//pressing SRC to BOut gout.close ()//Brush Cache byte Dest[]=bout.tobytearray ();//src==>dest//Summary: Set when output compresses data
    Response Head Resp.setheader ("content-encoding", "gzip"); After setting the response head, there is no need to download, not garbled Resp.setcontenttype ("text/html;charset=utf-8");//Be sure to set the Str.getbyte code//When the file is very small: compression no effect, but the burden (can not be offset Cost to eliminate compression) ...
    Larger files will be compressed (generally >200k) System.out.println ("Length before compression:" +src.length);
    SYSTEM.OUT.PRINTLN ("Compressed length:" +dest.length);
After compressing the data dest brush out OutputStream out=resp.getoutputstream ();//resp.getwriter ();
    Out.write (SRC);

  Out.write (dest);
 }

}

Download Picture: Downservlet. java (picture under SRC directory)

Package cn.hncu.servlet;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;

Import Java.net.URLEncoder;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; public class Downservlet extends HttpServlet {@Override protected void service (HttpServletRequest req, httpservletre Sponse resp) throws Servletexception, IOException {//Protocol Setup 1 Resp.setcontenttype ("Application/force-download"
    //////////download String filename= "4.jpg" with the application of the default download file;
    If the filename is in Chinese--such as: my picture. jpg//filename=urlencoder.encode (filename, "utf-8");//to encode the filename, if not encoded, the browser will display the file name garbled
    InputStream In=downservlet.class.getclassloader (). getResourceAsStream (FileName); Protocol Settings 2 Resp.setheader ("Content-disposition", "attachment;filename=\" +filename+ "\");//Tell the browser the current download file name//downserv Let.class's Location: "D:\apache-tomcat-7.0.30\webapps\helloWeb\WEB-INF\classEs\cn\hncu\servlet "//Picture's current position:" D:\apache-tomcat-7.0.30\webapps\helloWeb\WEB-INF\classes "//fileinputstream fin=
    New FileInputStream (fileName);//Not OutputStream out=resp.getoutputstream ();
    byte buf[]=new byte[512];
int len=0; while ((Len=fin.read (BUF))!=-1) {//Out.write (buf, 0, Len);//} while ((Len=in.read (BUF)!=-1) {OUT.WR
    ITE (buf, 0, Len);
 }
  }


}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.