如何在Java Spring MVC项目中从FTP服务器获取PDF文件并以Base64编码格式获得?

huangapple go评论76阅读模式
英文:

How can i get pdf files from FTP server as base64 encoding format on Java spring mvc project?

问题

public List<String> getMultipleBase64PDF(String workingDir) {
    List<String> fileList = new ArrayList<>();
    try {
        FTPFile[] files = client.listFiles(workingDir);
        for (FTPFile file : files) {
            if (file.getName().endsWith(".pdf")) {
                fileNames.add(file.getName());
            }
        }
        if (fileNames.size() > 0) {
            for (String filename : fileNames) {
                String encodedFile = "";
                InputStream is = client.retrieveFileStream(workingDir);
                File file = File.createTempFile("tmp", null);
                FileUtils.copyInputStreamToFile(is, file);
                byte[] bytes = new byte[(int) file.length()];
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(bytes, 0, (int) file.length());
                bis.close();
                encodedFile += new String(Base64.getEncoder().withoutPadding().encode(bytes), "UTF-8");
                fileList.add(encodedFile);
                file.delete();
                client.completePendingCommand();
            }
        }
        disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileList;
}

(Note: This code has been translated based on your request. If you have any specific questions about the code or its functionality, please feel free to ask.)

英文:

I'm trying getting files from FTP server on Java spring mvc project. I'm study on windows but my tomcat server is in linux machine. Following code returns base64 encoding files and created base64 url for front end side and this files temporarily held. This code works fine windows but works bad on linux machine. Getting pdf files are issueless in windows, corrupted in linux machine. Works differently according to file size.

This result in linux machine, this file is issueless in folder of FTP.
如何在Java Spring MVC项目中从FTP服务器获取PDF文件并以Base64编码格式获得?

This result on windows machine

如何在Java Spring MVC项目中从FTP服务器获取PDF文件并以Base64编码格式获得?

Can the problem be caused by base64 encoding?

public List&lt;String&gt; getMultipleBase64PDF(String workingDir) {
	List&lt;String&gt; fileList = new ArrayList&lt;&gt;();
	try {
		FTPFile[] files = client.listFiles(workingDir);
		for (FTPFile file : files) {
			if (file.getName().endsWith(&quot;.pdf&quot;)) {
				fileNames.add(file.getName());
			}
		}
		if (fileNames.size() &gt; 0) {
			for (String filename : fileNames) {
				String encodedFile = &quot;&quot;;
				InputStream is = client.retrieveFileStream( workingDir);
				File file = File.createTempFile(&quot;tmp&quot;, null);
				FileUtils.copyInputStreamToFile(is, file); 
				byte[] bytes = new byte[(int) file.length()];
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
				bis.read(bytes, 0, (int) file.length());
				bis.close();
				encodedFile += new String(Base64.getEncoder().withoutPadding().encode(bytes), &quot;UTF-8&quot;);
				fileList.add(encodedFile);
				file.delete();
				client.completePendingCommand();
			}
		}
		disconnect();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return fileList;
}

答案1

得分: 0

FTPClient默认设置为ASCII。在配置FTPClient bean时,尝试将客户端从使用ASCII切换到二进制,如下所示:

client.setFileType(FTP.BINARY_FILE_TYPE);

这里是类似问题的原始答案

英文:

FTPClient is set to ASCII by default. While configuring FTPClient bean, try switching your client from using ASCII to binary, like so:

client.setFileType(FTP.BINARY_FILE_TYPE);

Here's the original answer for the similair problem

huangapple
  • 本文由 发表于 2020年7月24日 19:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63072619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定