This article has shared examples of Java compressed files and downloaded images for your reference. The specific content is as follows
Main page index.xml
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html> <head> <title>Project's homepage</title> </head> <body> <h2>Homepage</h2> <h2>Changsha, Hunan</h2> <h3>Publish method one....</h3> <img src="images/1.jpg"/><!-- Relative path--> <br/> <!-- Absolute path--> <a href="/helloWeb/gzip">Page content compression demonstration--gzip</a><br/><br/> <a href="down">Download pictures</a><br/><br/> </body></html>
Compressed file: GzipServlet.java
Only when the compressed file is large enough can the compression overhead be offset and the compression is effectively compressed. Otherwise, the file size after compression of a very small file will become larger.
package cn.hncu.servlet;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.zip.GZIPOutputStream;import javax.servlet.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, HttpServletResponse resp) throws ServletException, IOException { String str="vuttyjhgyurcHunan Changsha SurdivsfAnhui Wuhu 890-80897 Maybe longer v will get used to this tvsduvgkjxhvnxzc.nlkcjsdfpeifniuq4ey8f048eyuyli" +"skchkxhckxzncnxclkjhasliduhasduisuugdlisgdlkjadhlksjhdlkasjdhasklhdlkjsahashdkskdjhkdjshkldsjhfkljd";// byte src[]=str.getBytes();// Use platform default encoding (GBK), if there is no ISO-8859-1 byte src[]=str.getBytes("utf-8"); // The data sent to the browser in the background is not recognized by the browser--need to download it, and it is garbled to open it with text. Therefore, you need to set the protocol ByteArrayOutputStream bOut=new ByteArrayOutputStream();//Memory stream GZIPOutputStream gOut=new GZIPOutputStream(bOut); gOut.write(src);//Press src to bOut gOut.close();//Flash cache byte dest[]=bOut.toByteArray();//src==>dest //Summary: When outputting compressed data, you need to set the response header resp.setHeader("Content-Encoding", "gzip"); //After setting the response header, you don't need to download it, and do not garbled resp.setContentType("text/html;charset=utf-8");//Be sure to set str.getByte to encode//When the file is very small: compression has no effect, it will be cumbersome (cannot offset the overhead required for compression).. Larger files will be compressed (usually > 200k) System.out.println("Length before compression:"+src.length); System.out.println("Length after compression:"+dest.length); //Swipe out the compressed data to dest OutputStream out=resp.getOutputStream();//resp.getWriter();// out.write(src); out.write(dest); }}Download the picture: DownServlet .java (the picture is under the 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.Servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Protocol Set 1 resp.setContentType("application/force-download"); //Use the application that downloads the file by default to download String fileName="4.jpg"; //If the file name is Chinese--such as: My picture.jpg //fileName=URLEncoder.encode(fileName,"utf-8");//Encode fileName. If not encoded, the file name displayed in the browser will be garbled.InputStream in=DownServlet.class.getClassLoader().getResourceAsStream(fileName); //Protocol setting 2 resp.setHeader("content-Disposition", "attachment;filename=/'"+fileName+"/'");//Tell the browser the current downloaded file name //DownServlet.class location: "D:/apache-tomcat-7.0.30/webapps/helloWeb/WEB-INF/classes/cn/hncu/servlet" //The current location of the picture: "D:/apache-tomcat-7.0.30/webapps/helloWeb/WEB-INF/classes" //FileInputStream fin=new FileInputStream(fileName);//Impossible 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.write(buf, 0, len); } }} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.