Download through FTP, modify the txt file to be downloaded

/**

* There are many records of inspection results, which are stored in an independent database server

* @param mapping

* @param form

* @param request

* @param response

* @return

*/

public ActionForward checkResultDown(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws JSchException {

 

String downDate = request.getParameter("downDate");

String selOrg = "";

String downFile = "";

try {

selOrg= URLDecoder.decode(request.getParameter("selOrg"),"UTF-8");

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

}

// file name rules that exist on the server: institution code_date

downFile = selOrg.split("_")[0]+"_"+downDate;

FTPClient ftpClient = new FTPClient();

try {

String hosts =  Constant.getStPara("hosts");

String user = Constant.getStPara("user2");

String pwd = Constant.getStPara("pwd");

 

String tmpPath = Constant.getStPara("tmpResultPath");

File f = new File(tmpPath);

if(!f.exists()){

f.mkdirs();

}

 

ftpClient.connect(hosts);

ftpClient.setControlEncoding("UTF-8");  

 

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);  

conf.setServerLanguageCode("zh");

//After FTP login, users will have their own initial configuration directory

ftpClient.login(user, pwd);

FTPFile[] fs = ftpClient.listFiles();

ftpClient.setBufferSize(1024);

// set file type (binary)

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

 

FileOutputStream fl = null;

 

for (int i = 0; i < fs.length; i++) {

FTPFile ff = fs [i];

String fileName  = ff.getName();

if (fileName.startsWith(selOrg)) {

 

ServletOutputStream outputStream = response.getOutputStream();

// This is the key code to pop up the download dialog

response.setHeader("Content-disposition","attachment;filename="+ fileName);

fl = new FileOutputStream(tmpPath+fileName);

ftpClient.retrieveFile(fileName, fl);

//Save the txt file on the FTP server to the local

fl.flush();

fl.close();

       BufferedReader in_=new BufferedReader(new FileReader(tmpPath+fileName));

       String line;

       int count=1;

       while((line=in_.readLine())!=null){

       if(count==1){

       outputStream.println("Rule ID, detection date, rule description, rule group ID, inspection topic, system source, inspection table, inspection column, inspection dimension, dimension subdivision, organization number, inspection total, passed number, failed number, error rate, pass rate");

       }

       outputStream.println(line);

           count++;

       }

       in_.close();

outputStream.flush();

outputStream.close();

break;

}

}

} catch (IOException e) {

e.printStackTrace ();

throw new RuntimeException(e.getMessage());

} finally {

try {

ftpClient.disconnect();

} catch (IOException e) {

e.printStackTrace ();

throw new RuntimeException("An exception occurred when closing the FTP connection!", e);

}

}

 

    return null;

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040257&siteId=291194637