java file with multiple labeled package to download example zip

2011-08-24  来源:本站原创  分类:Industry  人气:152 

/ / Download file package
public static HttpServletResponse downLoadFiles (List <File> files,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
/ ** This collection is packed all the files you want,
* It is assumed that ready to be packed files * /
/ / List <File> files = new ArrayList <File> ();

/ ** Create a temporary compressed files,
* We will file all injected into the stream file
* You can customize this file is defined. Rar or. Zip * /
File file = new File ("c: / certpics.rar");
if (! file.exists ()) {
file.createNewFile ();
}
response.reset ();
/ / Response.getWriter ()
/ / Create file output stream
FileOutputStream fous = new FileOutputStream (file);
/ ** Packaging of this method we will use an output stream ZipOutputStream,
* So here we change if the output stream * /
/ / Org.apache.tools.zip.ZipOutputStream zipOut
/ / = New org.apache.tools.zip.ZipOutputStream (fous);
ZipOutputStream zipOut
= New ZipOutputStream (fous);
/ ** This method is an acceptable set of files to be packaged,
* There is also a ZipOutputStream * /
zipFile (files, zipOut);
zipOut.close ();
fous.close ();
return downloadZip (file, response);
} Catch (Exception e) {
e.printStackTrace ();
}
/ ** Until the file has been successfully packaged, and
* I file packaging process is encapsulated in FileUtil.zipFile this static method,
* Later revealed, the next is to write the data to the client * /
/ / OutputStream out = response.getOutputStream ();

return response;
}

/ **
* Acceptance of all the files into the archive
* @ Param List <File>;
* @ Param org.apache.tools.zip.ZipOutputStream
* /
public static void zipFile
(List files, ZipOutputStream outputStream) {
int size = files.size ();
for (int i = 0; i <size; i + +) {
File file = (File) files.get (i);
zipFile (file, outputStream);
}
}

public static HttpServletResponse downloadZip (File file, HttpServletResponse response) {
try {
/ / Stream in the form of download.
InputStream fis = new BufferedInputStream (new FileInputStream (file.getPath ()));
byte [] buffer = new byte [fis.available ()];
fis.read (buffer);
fis.close ();
/ / Clear the response
response.reset ();

OutputStream toClient = new BufferedOutputStream (response.getOutputStream ());
response.setContentType ("application / octet-stream");
response.setHeader ("Content-Disposition", "attachment; filename =" + file.getName ());
toClient.write (buffer);
toClient.flush ();
toClient.close ();
} Catch (IOException ex) {
ex.printStackTrace ();
} Finally {
try {
File f = new File (file.getPath ());
f.delete ();
} Catch (Exception e) {
e.printStackTrace ();
}
}
return response;
}

/ **
* Based on the input and output stream file package to the file
* @ Param File
* @ Param org.apache.tools.zip.ZipOutputStream
* /
public static void zipFile (File inputFile,
ZipOutputStream ouputStream) {
try {
if (inputFile.exists ()) {
/ ** If there is a directory, then do not take action,
* The package is being studied in the directory * /
if (inputFile.isFile ()) {
FileInputStream IN = new FileInputStream (inputFile);
BufferedInputStream bins = new BufferedInputStream (IN, 512);
/ / Org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry (inputFile.getName ());
ouputStream.putNextEntry (entry);
/ / Output data to the compressed file
int nNumber;
byte [] buffer = new byte [512];
while ((nNumber = bins.read (buffer))! = -1) {
ouputStream.write (buffer, 0, nNumber);
}
/ / Close the stream object created
bins.close ();
IN.close ();
} Else {
try {
File [] files = inputFile.listFiles ();
for (int i = 0; i <files.length; i + +) {
zipFile (files [i], ouputStream);
}
} Catch (Exception e) {
e.printStackTrace ();
}
}
}
} Catch (Exception e) {
e.printStackTrace ();
}
}

相关文章
  • java file with multiple labeled package to download example zip 2011-08-24

    / / Download file package public static HttpServletResponse downLoadFiles (List <File> files, HttpServletRequest request, HttpServletResponse response) throws Exception { try { / ** This collection is packed all the files you want, * It is assumed t

  • Transfer: Multiple jar package labeled a large jar package 2010-04-27

    ant's build.xml major labels are as follows: (1) <mkdir> create a folder (1) <copy> copy files (2) <javac> compile java file (3) <jar> packaging class files (3) <unzip> decompress jar package There are many specific Internet

  • Java file download in several ways 2008-05-15

    Java file download in several ways public HttpServletResponse download(String path, HttpServletResponse response) { try { // path Refers to download the file path . File file = new File(path); // Get the file name . String filename = file.getName();

  • java file name to resolve 2010-12-09

    java code for the text is unicode based, therefore, if the ZipInputStream and ZipOutputStream to handle compression and decompression of the work, met in the document name or path, that of course is to deal with unicode Lo! But now the market compres

  • Java file using the JAD decompiler good light JAVA _ 2011-01-09

    Java file using the JAD decompiler September 2, 2007 Sunday, 02:22 AM Recently drawn the short straw, the computer system unit accidentally hang out, and restore the system also failed, the most terrible is when colleagues in restoring my machine to

  • Garbled Eclipse JAVA file comments 2010-03-29

    For someone else's project or JAVA files into its own when Eclipse often JAVA file will appear garbled in Chinese notes the case, largely because of someone else's IDE encoding format and encoding format different from their own Eclipse. Can refer to

  • java file write operation 2010-03-29

    package com.test2; import java.io.*; /** * @desc java File write operations * @author zhaozhi3758 * */ public class FileWriteT { // The following method file is created automatically public void writeToFile1(String path,String str) throws FileNotFoun

  • java how to play JAR package (transfer 2010-02-03

    Keywords: jar jar application to first open a command prompt (win2000 or in the implementation of the cmd command to run basket, win98 as the DOS prompt), type jar-help, and then enter (if you already have plate jdk1.1 or above), see what: Usage: jar

  • Eclipse set the java file of the author, date, etc. Note 2010-07-26

    Window-"Ppreference-" Java-"Code Styple-" Code Templates The right point to open Comments, select Files, click the edit: Java code /************************************************* ********************** * * @ Package: $ (package_name

  • Eclipse set the java file of the author, date and other instructions 2010-07-26

    Window-"Ppreference-" Java-"Code Styple-" Code Templates The right point to open Comments, select Files, click the edit: Java code /************************************************* ********************** * * @ Package: $ (package_name

  • java proxy --- the first step, dynamic compilation java file 2010-10-16

    Assume that we have a dynamically generated string type the following statement of a class String src = "package com.wjj.myproxy;" + "Public class CarTimeProxy implements Moveable {" + "Moveable car = null;" + "Public Ca

  • swfUpload java file upload component upload.war 2010-10-28

    swfUpload java file upload component upload.war placed directly under the webapps directory. Upload file exists webapps \ upload \ files directory upload_src.rar Eclipse project source code Support for multiple file upload.

  • java file encoding conversion 2010-12-03

    package com.xw.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream

  • How to use CXF. Wsdl file into a java file 2011-07-27

    How to use CXF. Wsdl file into a java file Have not previously had contact with webservice development, the two barely beginning to see, customers. Wsdl file, I want to convert. Java files in the Internet is a lot of information, and finally pass the

  • 基于java file 文件操作operate file of java的应用 2014-06-19

    本篇文章介绍了,基于java file 文件操作operate file of java的应用.需要的朋友参考下 java文件操作 package com.b510; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.PrintWrite

  • Java file operations method 2010-03-29

    Transfer from the following code: http://lionelxx777.javaeye.com/blog/358128 Java file operations to the text of the two methods. 1. The usual practice is to use FileInputStream and FileOutputStream to carry out operations, the above two categories r

  • Java file operations enhancement tools 2010-03-29

    Java file operations enhancement tools -------------------------------------------------- ------------------------------ 2008-12-15 18:30:43 Tags: File IO commons [pushed to the technical ring] Copyright Statement: original works, and if reproduced,

  • encoding one of the Java file encoding 2010-05-08

    1. Edit *. java file what happened then? A: When we use Notepad to edit, Open the purple input, purple keyboard input software control from the keyboard to become a phonetic alphabet, alphabet below shows the GBK Simplified or Traditional determined

  • Java file operations on the Properties Summary 2010-05-13

    Frequently used, Java file operations on the Properties, to make a summary here. Java Properties files on the operation to be "unsatisfactory", said here about the first time in general, back slowly said. 1.java read Properties files, often worn

  • Java file in Notepad and the difference between line (turn) 2010-10-27

    And today they made a java file reading and writing about the issue, originally thought to java database ok, but now look, the file read and write, thread and other issues are all very important, ah! ! Oh, well cut to the chase, today's problem is th