簡體   English   中英

打開保存在FTP服務器上的文件

[英]Opening file saved on FTP server

我有添加附件的功能。 當我們附加任何文件時,它將被上傳到FTP服務器。 出於查看目的,在瀏覽器本身中有一個鏈接顯示來自FTP服務器的文件內容。 從FTP服務器下載文件后,我可以打開文件,但是我不想在本地計算機上下載或保存文件。 有什么方法可以從FTP服務器打開文件,而無需在本地計算機上下載或保存文件?

我寫的代碼。

FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpServerIp);
boolean connectionStatus = ftpClient.login(ftpServerUserName, ftpServerPassword);
if(connectionStatus){
    log.info("Connected successfully.");
}

ftpClient.changeWorkingDirectory(ftpServerUploadPath);
fos = new FileOutputStream(fileName);
boolean result = ftpClient.retrieveFile(fileName, fos);
log.info("result==>"+result);

在這里,獲取文件時的結果為true 但我無法顯示文件內容。 有什么辦法可以實現? 任何幫助表示贊賞。 提前致謝。

我已經通過使用下面的代碼解決了這個問題。

FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpServerIp);
boolean connectionStatus = ftpClient.login(ftpServerUserName,    ftpServerPassword);
if(connectionStatus){
    log.info("Connected successfully.");
}
ftpClient.changeWorkingDirectory(ftpServerUploadPath);
InputStream stream = ftpClient.retrieveFileStream(fileName);
int length   = 0;
ServletContext      context  = getServletConfig().getServletContext();
String              mimetype = context.getMimeType( fileName );
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileName + "\"" );

byte[] bbuf = new byte[fileName.length()];
DataInputStream in = new DataInputStream(stream);

while ((in != null) && ((length = in.read(bbuf)) != -1))
{
    out.write(bbuf,0,length);
}
in.close();
out.flush();
out.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM