java code implementation file download function

Yesterday, according to the requirements of the requirements document, they ought to do a feature on the downloaded file, graduated from the school has been a long, long time myself have not done this, so own the Internet Baidu, eventually developed code is as follows:

Oh! Yes, I would like to talk about my ideas, you first need to get the path to store files on the server, and then download the file locally. It is the first query in the file table path.

LPEdorAnnex table :( this is their use of the table - a field which is used AnnexParth)

 

Here is your code, wrote in a jsp page, as if written in the java class, I do not know how to engage is not very good, and definitely the main reason is not because of his strength, but also because it was their own hurry, no so many events carefully studied, if there is time in the future, then we must take a good look down.

  . 1 <% -
   2    . IntelliJ the Created by IDEA
   . 3    the User: as Blame
   . 4    a Date: 2019/8/29
   . 5    Time: 10:49
   . 6    the To Change the this Template use File | Settings | . File the Templates
   . 7    function: the Problems member issued
   8 -%>
   9 <%
 10  @ program name:
 11  @ program features: preservation - issued problems member
 12  // Create date: 2019/09/09
 13  // created:
 14  // update record: updated updated update Cause / content 
15 %>
 16 <% @ Page Import="com.sinosoft.utility.TransferData" %>
 17 <%@ page import="com.sinosoft.utility.VData" %>
 18 <%@ page import="com.sinosoft.service.BusinessDelegate" %>
 19 <%@ page import="com.sinosoft.lis.schema.LPPolicyHolderSchema" %>
 20 <%@ page import="com.sinosoft.lis.mgubq.PEdorTypeACUI" %>
 21 <%@ page import="com.sinosoft.utility.CErrors" %>
 22 <%@ page import="org.apache.log4j.Logger" %>
 23 <%@ page import="com.sinosoft.lis.mgubq.BQQuestionFileIssueUI" %>
 24 <%@ page import="java.io.*" %>
 25 <%@ page import="org.apache.jasper.JasperException" %>
 26 <%@page contentType="text/html;charset=utf-8" %>
 27 <%@include file="../common/jsp/UsrCheck.jsp" %>
 28 <%
 29     //输出参数
 30     CErrors tError = null;
 31     String FlagStr = "Fail";
 32     String Content = "";
 33     GlobalInput tGI = new GlobalInput();
 34     tGI = (GlobalInput) session.getAttribute("GI");
 35     TTransferData = TransferData new new TransferData ();
 36  
37 [      // page validity determination 
38 is      IF (TGI == null ) {
 39          FlagStr = "the Fail" ;
 40          the Content = "invalid page, re-landing" ;
 41 is          Logger.getLogger ( " BQQuestionFileIssueSave ".) info (" invalid page, re-landing " );
 42 is      } the else {
 43 is          // save login administrator account 
44 is          String the operator = tGI.Operator;
 45          // fmtransact operator
 46          // get foreground data 
47         Request.getParameter tOperate = String ( "fmtransact" );
 48          // get information page entry 
49          tTransferData.setNameAndValue ( "OtherNo", request.getParameter ( "OtherNo")); // policy number √ 
50          tTransferData.setNameAndValue ( " QuestionType ", request.getParameter (" QuestionType ")); // problem type member √ 
51 is          tTransferData.setNameAndValue (" QuestionAttribute ", request.getParameter (" QuestionAttribute ")); // problem attribute member √ 
52 is          tTransferData.setNameAndValue (" QuestionReasion ", request.getParameter (" QuestionReason ")); // problems pieces reasons √ 
53         tTransferData.setNameAndValue("QuestionSendEmail", request.getParameter("sendemail"));//下发邮箱 √
 54         tTransferData.setNameAndValue("QuestionDes", request.getParameter("QuestionDes"));//问题件描述 √
 55 
 56         tTransferData.setNameAndValue("QuestionID", request.getParameter("QuestionID"));
 57         tTransferData.setNameAndValue("sendnode", request.getParameter("sendnode"));
 58         tTransferData.setNameAndValue("sender", request.getParameter("sender"));
 59         tTransferData.setNameAndValue("senddate", request.getParameter("senddate"));
 60         tTransferData.setNameAndValue("replier", request.getParameter("replier"));
 61         tTransferData.setNameAndValue("replycontent", request.getParameter("replycontent"));
 62         tTransferData.setNameAndValue("replydate", request.getParameter("replydate"));
 63         tTransferData.setNameAndValue("state", request.getParameter("state"));
 64         tTransferData.setNameAndValue("FilePath", request.getParameter("FilePath"));//问题件附件下载路径
 65 
 66         tTransferData.setNameAndValue("Operator",tOperate);
IF68problems downloading attachments//67         
         ( "downloadQuestion" .equals (tOperate)) {
 69              the try {
 70                  String filePath = request.getParameter ( "FilePath" );
 71 is  
72                  // upload the document to the affected server, the server configured to obtain impact zhuting path by 
73 is                  String the tpath = "" ;
 74                  String TFileName = "" ;
 75                  String SQL = "" ;
 76  
77  
78                  Boolean tFileExists = to true ;
 79                  System.out.println ( "to download the file name:" + filePath);
 80                 File file = new File(filePath);
 81                 if(!file.exists()){
 82                     tFileExists=false;
 83                 }
 84                 if(tFileExists) {
 85                     InputStream fi = new FileInputStream(file);
 86                     //BufferedOutputStream处理txt格式时多出2行空行
 87                     response.reset();
 88                     response.setContentType("application/x-download");
 89                     response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
 90 
 91                     OutputStream outOS = response.getOutputStream();
 92                     BufferedOutputStream bos = new BufferedOutputStream(outOS);
 93 
 94                     int start;
 95                     byte[] bt = new byte[4096];
 96                     while ((start = fi.read(bt)) != -1) {
 97                         bos.write(bt, 0, start);
 98                     }
 99 
100                     bos.flush();
101                     fi.close();
102                     bos.close ();
 103                      outOS.close ();
 104                      out.clear ();       // empty the contents of the cache 
105                      out pageContext.pushBody = ();   // updates the PageContext out property 
106  
107  
108  
109                  }
 110              } the catch (Exception E) {
 111  
112              }
 113  
114          }
 115          // call-up UI class 
1 16          the try {
 117              // preparing the transmission data VData 
1 18              VData = tVDatanew new VData ();
 119              tVData.add (tTransferData);
 120              tVData.add (TGI);
 121              BusinessDelegate tBusinessDelegate = BusinessDelegate.getBusinessDelegate ();
 122              // write temporarily not call UI class, so the first direct call-up UI class approach
 123              //     IF (! tBusinessDelegate.submitData (tVData, tOperate, "PEdorTypeACUI")) { 
124              BQQuestionFileIssueUI tBQQuestionFileIssueUI = new new BQQuestionFileIssueUI ();
 125              IF (! {tBQQuestionFileIssueUI.submitData (tVData, tOperate))
 126                 Content = "data to save failed due to:." + TBQQuestionFileIssueUI.getErrors () the getError (0 ) .errorMessage;
 127                  FlagStr = "the Fail" ;
 128              } the else {
 129                  FlagStr = "Success" ;
 130.                  Content = "processing is successful! " ;
 131 is              }
 132          } the catch (exception EX) {
 133              ex.printStackTrace ();
 134              the Content = FlagStr +" handling exceptions, system maintenance personnel contact! " ;
 135              FlagStr =" the Fail " ;
136         }
137 
138     }
139 
140 %>
141 <html>
142 <script type="text/JavaScript">
143     try {
144         parent.fraInterface.afterSubmit('<%=FlagStr%>', '<%=Content%>');
145     }
146     catch (ex) {
147         <%--alert('<%=tContent%>');--%>
148     }
149 </script>
150 </html>

Note: If you do not add the following two lines of code words

    out.clear (); // clear the contents of the cache 
    out = pageContext.pushBody (); // attribute updates out of PageContext

Will report getOutputStream () has already been called for this response anomalies, annoying people, but the strange thing is: can actually download the file, but the last is the console will report this error, Baidu said its own version of the problem tomcat, with linux system will not have this problem, of course, there will always be problems have solutions of. After adding two lines of code, it would not report such a mistake.

 

 

Guess you like

Origin www.cnblogs.com/dongyaotou/p/11546724.html