英文:
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.
This result on windows machine
Can the problem be caused by base64 encoding?
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;
}
答案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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论