java - 在 Java Struts Web 应用程序中使用 JSch 上传文件被损坏

标签 java inputstream sftp struts jsch

上传文件时,我没有遇到任何异常,但文件无法打开,因为它已损坏。

我们在应用程序中使用 Struts 1.x。

我认为输入流会出现一些问题。它只读取文本文件。

如果我上传 pdf 或 xls 文件,它不会打开并显示消息“文件已损坏或扩展名已更改”。

谁能告诉我解决办法吗?

从操作类上传方法:

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");
    }  
}

这是使用 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;
}

最佳答案

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

上面的代码读取并丢弃上传文件中的第一个字节。所以FTP上传是从第二个字节开始的。删除该代码。

关于java - 在 Java Struts Web 应用程序中使用 JSch 上传文件被损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58730754/

相关文章:

java - 即使关闭流后也无法删除文件

java - 无法从非空InputStream读取内容

java - 问题是 while 循环没有以 Java 中的 system.in 获取输入段落结束

macos - VIM FTP 保存后自动上传?

linux - 基于简单 shell/bash 的 sftp 脚本将文件上传到目标服务器的根目录

Java Arraylist 大小声明和性能

java - 我如何在 Liferay 上获取用户的名字?

java - 嵌套 for 循环打印数组列表中的一组值

linux - Qt creator 无法上传文件到远程设备

java - 动态规划 - 做出改变