本文實例為大家分享了java壓縮文件和下載圖片示例,供大家參考,具體內容如下
主頁面index.xml
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html> <head> <title>項目的主頁</title> </head> <body> <h2>主頁主頁</h2> <h2>湖南長沙</h2> <h3>發布方式一......</h3> <img src="images/1.jpg"/><!-- 相對路徑--> <br/> <!-- 絕對路徑--> <a href="/helloWeb/gzip">頁面內容壓縮演示--gzip</a><br/><br/> <a href="down">下載圖片</a><br/><br/> </body></html>
壓縮文件:GzipServlet.java
只有被壓縮文件足夠大,才能抵消壓縮開銷,有效壓縮。否則,很小的文件壓縮之後的文件大小反而變大
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, HttpServletResponse resp) throws ServletException, IOException { String str="vuttyjhgyurc湖南長沙surdivsf安徽蕪湖890-80897也許更長v就會習慣這樣tvsduvgkjxhvnxzc.nlkcjsdfpeifniuq4ey8f048eyuyli" +"skchkxhckxzncnxclkjhasliduhasiduisugdlisgdlkjadhlksjhdlkasjdhasklhdlkjsahashdkskdjhkdjshkldsjhlksjhfkljd";// byte src[]=str.getBytes();//使用平台默認編碼(GBK),沒有就使用ISO-8859-1 byte src[]=str.getBytes("utf-8"); //後台向瀏覽器發的數據,瀏覽器不認識--需要下載,用文本打開是亂碼。所以要設置協議ByteArrayOutputStream bOut=new ByteArrayOutputStream();//內存流GZIPOutputStream gOut=new GZIPOutputStream(bOut); gOut.write(src);//把src壓到bOut gOut.close();//刷緩存byte dest[]=bOut.toByteArray();//src==>dest //總結:輸出壓縮數據時要設置響應頭resp.setHeader("Content-Encoding", "gzip"); //設置響應頭之後,就不用下載,不亂碼resp.setContentType("text/html;charset=utf-8");//一定要把str.getByte設置編碼//當文件很小時:壓縮無效果,反而累贅(不能抵消壓縮所需要的開銷)...較大文件會被壓小(一般>200k) System.out.println("壓縮前的長度:"+src.length); System.out.println("壓縮後的長度:"+dest.length); //把壓縮之後的數據dest刷出去OutputStream out=resp.getOutputStream();//resp.getWriter();// out.write(src); out.write(dest); }}下載圖片:DownServlet .java(圖片在src目錄下面)
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, HttpServletResponse resp) throws ServletException, IOException { //協議設置1 resp.setContentType("application/force-download");//用默認下載文件的應用程序下載String fileName="4.jpg"; //如果文件名是中文--如:我的圖片.jpg //fileName=URLEncoder.encode(fileName,"utf-8");//把fileName編碼一下,如果不編碼,瀏覽器中顯示的文件名會亂碼InputStream in=DownServlet.class.getClassLoader().getResourceAsStream(fileName); //協議設置2 resp.setHeader("content-Disposition", "attachment;filename=/'"+fileName+"/'");//告訴瀏覽器當前下載的文件名//DownServlet.class的位置:"D:/apache-tomcat-7.0.30/webapps/helloWeb/WEB-INF/classes/cn/hncu/servlet" //圖片所在的當前位置:"D:/apache-tomcat-7.0.30/webapps/helloWeb/WEB-INF/classes" //FileInputStream fin=new FileInputStream(fileName);//不可行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); } }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。