如何在java structs中动态创建文件

83qze16e  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(176)

我需要创建一个csv文件的基础上数组列表值和如何上传到ftp服务器我需要文件得到创建的飞行和传递它ftp方法,使它得到上传到ftp服务器

public ActionForward generatePayroll(ActionMapping mapping, ActionForm form,
                       HttpServletRequest request, HttpServletResponse response) {
   SessionInfoForm _form = (SessionInfoForm) form;
   SisTransactionsSession _config = _form.getSisTransactionsSession();
   String loggedInUser = _form.getLoggedinEmployeeDVO().getLoginId().toUpperCase();
   String[] business = request.getParameterValues("selectedBusinessValues");
   String[] position = request.getParameterValues("selectedPositionValues");
   File file= new File("PAYROLL_PRM.csv");
   InputStream isFixedValue = null;
   try {
      // create FileWriter object with file as parameter
      FileWriter fileWriter = new FileWriter(file);
      // add data to csv
      fileWriter.write(business.toString());
      fileWriter.write(position.toString());

      // closing writer connection
      //fileWriter.flush();
      isFixedValue = new BufferedInputStream(fileWriter.getInputStream());
      fileWriter.close();
   }
   catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
   return mapping.findForward("success");
}

下面的方法工作上传文件到ftp服务器

private boolean fileUpload(InputStream isUploadFile, String dirName, String loggedInUser,  String fileName){
     boolean storeRetVal = false;
  String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
     storeRetVal = SISSFTPManager.getInstance().put(isUploadFile, dirName, fileName);
     if (storeRetVal)
     {
         try {
        if (fileType.trim().equalsIgnoreCase("csv")){
           ICSAPI.getInstance().getSIMSOrderManager().createFileAudit(loggedInUser, fileName);
        } else {

        }
   } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (SystemException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (ApplicationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
     logger.info("BulkUploadAction:fileUpload SFTP Transfer file successfully!");
     } else {
         logger.error("BulkUploadAction:fileUpload SFTP Transfer file FAILED!");          
     }       

  return storeRetVal;
 }
bxfogqkk

bxfogqkk1#

您可以使用ByteArrayOutputStreamByteArrayInputStream来得到InputStream。您不需要创建文件。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (Writer writer = new OutputStreamWriter(bos)) {
    writer.write(business.toString());
    writer.write(position.toString());
}
InputStream inputStream = new ByteArrayInputStream(bos.toByteArray());

相关问题