fileinputstream - create and download the Zip file java -


in application there no of documents(pdf) particular tender. need create zip file pdf files , allow user download it.

application done in javaee struts , mysql. when user clicks download button action class gets called. code not give exceptions not prompt user download either.

please me find wrong in code.

following source code of action class..

public class actdownloaddoczip extends action { static logger logger = logger.getlogger(actdownloaddoczip.class);  public actionforward execute(actionmapping mapping, actionform form,         httpservletrequest request, httpservletresponse response)         throws exception {     string realpath = getservlet().getservletcontext().getrealpath(             "/web-inf/log4jconfiguration.xml");     domconfigurator.configure(realpath);     logger.info("in actdownloaddoczip....");     actionforward forward = null;     httpsession session = request.getsession();     // db connection     connection conn = null;     string[][] namefile = null;     string tenderno = "";     try {         conn = proxoolconnection.getproxoolconnectionslt();         tenderno = request.getparameter("tenderno");         // file fileex=new file("xxx.zip");         fileoutputstream zipfile = new fileoutputstream(new file("xxx.zip"));         zipoutputstream output = new zipoutputstream(zipfile);         // call getpdffiles method here         iloadtenders ld = new loadtenders();         namefile = ld.getpdffilelisttotender(conn, tenderno);//this method brings relevant pdf file names , paths((pdfname1,pdfpath1),(pdfname2,pdfpath2))         (int = 0; < namefile.length; i++) {             zipentry zipentry = new zipentry(namefile[i][0].trim());             output.putnextentry(zipentry);             fileinputstream pdffile = new fileinputstream(new file(                     namefile[i][1].trim()));             ioutils.copy(pdffile, output);              pdffile.close();             output.closeentry();         }         output.finish();         output.close();     } catch (sqlexception e) {         system.out.println("actdownloaddoczip " + e);         logger.fatal(e.getmessage());     } catch (exception e) {         system.out.println("actdownloaddoczip1 " + e);         logger.fatal(e.getmessage());     } {         if (conn != null) {             proxoolconnection.closeproxoolconnectionslt(conn);         }     }     forward = mapping.findforward("publicdashboard");     return forward;   } } 

done :) there... had give proper path , download it....

here action class again:

    package com.affno.etender.frontend;  import java.io.bufferedoutputstream; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.sql.connection; import java.sql.sqlexception; import java.util.zip.zipentry; import java.util.zip.zipfile; import java.util.zip.zipoutputstream;  import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession;  import org.apache.commons.io.ioutils; import org.apache.log4j.logger; import org.apache.log4j.xml.domconfigurator; import org.apache.struts.action.action; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping; import org.apache.struts.util.messageresources;  import com.affno.util.proxool.proxoolconnection;  public class actdownloaddoczip extends action {     static logger logger = logger.getlogger(actdownloaddoczip.class);      public actionforward execute(actionmapping mapping, actionform form,             httpservletrequest request, httpservletresponse response)             throws exception {          string realpath = getservlet().getservletcontext().getrealpath(                 "/web-inf/log4jconfiguration.xml");         domconfigurator.configure(realpath);         logger.info("in actdownloaddoczip....");          actionforward forward = null;         httpsession session = request.getsession();          // db connection         connection conn = null;          string[][] namefile = null;          string tenderno = "";          ////         fileoutputstream fos = null;         zipoutputstream zipout = null;         fileinputstream fis = null;          messageresources messages = messageresources.getmessageresources("resources.upload");         string dirzip = messages.getmessage("save.dir");         tenderno = request.getparameter("tenderno").trim();         string zipfoldername =tenderno + ".zip";         string filepathzipped = dirzip + zipfoldername;          try {             conn = proxoolconnection.getproxoolconnectionslt();                fos = new fileoutputstream(filepathzipped);             zipout = new zipoutputstream(new bufferedoutputstream(fos));              iloadtenders ld = new loadtenders();             namefile = ld.getpdffilelisttotender(conn, tenderno);              (int = 0; < namefile.length; i++){                 file input = new file(namefile[i][1].trim());                 fis = new fileinputstream(input);                 zipentry ze = new zipentry(input.getname());                 system.out.println("zipping file: "+input.getname());                 zipout.putnextentry(ze);                 byte[] tmp = new byte[4*1024];                 int size = 0;                 while((size = fis.read(tmp)) != -1){                     zipout.write(tmp, 0, size);                 }                 zipout.flush();                 fis.close();             }             zipout.close();             system.out.println("done... zipped files...");             //zip created              //downloading zip             file file = new file(filepathzipped);             if(!file.exists()){                 system.out.println("file not found");             }             response.setcontenttype("application/octet-stream");             response.setheader("content-disposition","attachment; filename=\"" + zipfoldername + "\"");              outputstream out = response.getoutputstream();             fileinputstream in = new fileinputstream(file);             byte[] buffer = new byte[4096];             int length;             while ((length = in.read(buffer)) > 0){                out.write(buffer, 0, length);             }             in.close();             out.flush();          }         ////          catch (sqlexception e) {             system.out.println("actdownloaddoczip " + e);             logger.fatal(e.getmessage());         } catch (exception e) {             system.out.println("actdownloaddoczip1 " + e);             logger.fatal(e.getmessage());         } {             if (conn != null) {                 proxoolconnection.closeproxoolconnectionslt(conn);             }         }          forward = mapping.findforward("publicdashboard");         /*          * if (forward != null) { forward = new actionforward(forward.getpath()          * +"servlet/downloadservlet?" + zipfile, true); }          */         return forward;      } } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -