User Tools

Site Tools


mirnshi:a_java_class_to_get_a_compressed_file_from_the_server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

mirnshi:a_java_class_to_get_a_compressed_file_from_the_server [2009/12/19 05:22] (current)
mirnshi created
Line 1: Line 1:
 +====== A java class to get a compressed file from the server ======
 +
 +Some guys often ask me to help them to get some files from the server, such as tomcat log. Everytime I must open ftp client to connect the server, download these file. The lazy man need a smart way to do this job. Now they can help themselves.
 +//
 +//
 +   ​import java.io.File;​
 +   ​import java.io.FileInputStream;​
 +   ​import java.io.IOException;​
 +   ​import java.io.InputStream;​
 +   ​import java.io.OutputStream;​
 +   ​import java.util.zip.*;​
 +   ​import javax.servlet.http.HttpServletRequest;​
 +   ​import javax.servlet.http.HttpServletResponse;​
 +   ​public class ZGet {
 +       ​public void get(HttpServletRequest request, HttpServletResponse response, String filename) {
 +           try {
 +               // String fname = request.getParameter("​xpath"​);​
 +               fname = filename;
 +               File f = new File(fname);​
 +               if (f.canRead()) {
 +                   ​response.setContentType("​application/​x-gzip"​);​
 +                   ​response.setHeader("​Location",​ f.getName());  ​
 +                   ​response.setHeader("​Content-Disposition",​ "​attachment;​ filename="​ + f.getName() + "​.gz"​);​
 +                   ​get(new FileInputStream(filename),​ response.getOutputStream());​
 +                   ​response.getOutputStream().flush();​
 +                   ​response.getOutputStream().close();​
 +                }
 +            } catch (Exception e) {
 +                // TODO Auto-generated catch block
 +                //​e.printStackTrace();​
 +            }  ​
 +        }
 +        private void get(InputStream in, OutputStream out) throws IOException {
 +            if (in == null || out == null) {
 +                return;
 +            }
 +            byte[] buffer = new byte[1024];
 +            int i = -1;
 +            GZIPOutputStream gzip = new GZIPOutputStream(out);​
 +            while ((i = in.read(buffer)) != -1) {
 +                gzip.write(buffer,​ 0, i);
 +            }
 +            gzip.close();​
 +        }
 +    }
 +
 +
 +
 +{{tag>}}
 +
 +~~LINKBACK~~
 +~~DISCUSSION~~
  
mirnshi/a_java_class_to_get_a_compressed_file_from_the_server.txt ยท Last modified: 2009/12/19 05:22 by mirnshi