Search code examples
javainputstreamsftpstrutsjsch

File uploading getting corrupted using JSch in Java Struts web application


While uploading files I am not getting any exceptions, but the file won't open because it's corrupted.

We are using Struts 1.x in our application.

I think with the input stream some issues will be there. It was reading only text files.

If I upload pdf or xls files it's not getting open displaying message 'file either damaged or extension changed'.

Can anyone tell me the solution please?

Upload method from action class:

public static void uploadFiles(FormFile[] formFile, FilesVO fVO) 
throws BaseException {
    Logger logger = LogManager.getLogger();
    boolean isSaved = false;
    int port=ATAConstants.SERVER_PORT;

    FileStorageVO fsVO = null;
    try {            
        if ( ( null != formFile[0].getFileName()
            && !formFile[0].getFileName().trim().equalsIgnoreCase(""))
            || ( null != formFile[1].getFileName()
            && !formFile[1].getFileName().trim().equalsIgnoreCase(""))
            || ( null != formFile[2].getFileName()
            && !formFile[2].getFileName().trim().equalsIgnoreCase(""))
            ) {

            fsVO = SFTPFileManager.getFileStorageDetails(SFTPFileManager.FILE_CATEGORY_WORKORDER);                
            JSch jsch = new JSch();
            Session session = null;
            session = jsch.getSession(fsVO.getSAccountName(),fsVO.getSFTPHostName(),port);

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setPassword(fsVO.getSPassword());
            session.connect();
            ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
            sftp.connect();
            if (sftp.isConnected()==true) {
                for (int i = 0; i < formFile.length; i++) {
                    if (null!=formFile[i] && null != formFile[i].getFileName()
                        && !formFile[i].getFileName().trim().equalsIgnoreCase("")) {
                        int iFileCount = CommonDAO.getFileCount().intValue();
                        String sActualFileName = formFile[i].getFileName();
                        String sDestinationStorageFileName =
                                SFTPFileManager.getStorageFileName(
                                fVO.getSFileCategory(),
                                fVO.getEntityId(),
                                iFileCount++,
                                sActualFileName);
                        isSaved =SFTPFileManager.uploadFile(sftp,
                                fsVO.getSFilePath(),
                                formFile[i],
                                sDestinationStorageFileName);            
                        fVO.setActualFileName(sActualFileName);
                        fVO.setStorageFileName(sDestinationStorageFileName);
                        fVO.setStorageId(ATAConstants.STORAGE_ID_WORKORDER);
                        if (isSaved) {
                            CommonDAO.addFiles(fVO);
                        }
                    }
                }
            }
            sftp.exit();
            sftp.disconnect();
        }
    }catch (JSchException   e) {
        logger.error("Error while uploading the file", e);
        throw new BaseException(e, "Exception occured while uploading file");
    }catch (SftpException e) {
        logger.error("Error while uploading the file", e);
        throw new BaseException(e, "Exception occured while uploading file");
    } catch (IOException e) {
        logger.error("Error while uploading the file", e);
        throw new BaseException (e, "Exception occured while uploading file");
    }  
}

This is file upload method using channel sftp sftpFileManager.java

public static boolean uploadFile(ChannelSftp sftp, String sDestinationFolder,
            FormFile fUploadFile, String sDestinationStorageFileName)
            throws IOException, FileNotFoundException, SftpException {

    boolean isSaved = false;
    sftp.cd(sDestinationFolder);`enter code here`
    InputStream is = fUploadFile.getInputStream();

    if(is.read()!=-1){
        sftp.put(is, sDestinationStorageFileName);
        isSaved = true;

    }
    is.close();    

    return isSaved;
}

Solution

  • if(is.read()!=-1){
    

    The above reads and discards the first byte from the uploaded file. So the FTP upload starts from the second byte. Remove that code.