在http協(xié)議下,實(shí)現(xiàn)下載一般就兩種方法,一個采用cont-type="";此種方法為附件的方式下載;;
站在用戶的角度思考問題,與客戶深入溝通,找到邢臺縣網(wǎng)站設(shè)計與邢臺縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋邢臺縣地區(qū)。
另一種較簡單,就是你只需要點(diǎn)下載按鈕然后跳轉(zhuǎn)到服務(wù)器的那個文件路勁就可以了,瀏覽器自動回進(jìn)行下載..
public?static?void?downloadFileFtp(KmConfig?kmConfig,String?fileName,?String?clientFileName,?OutputStream?outputStream){
try?{
String?ftpHost?=?kmConfig.getFtpHost();
int?port?=?kmConfig.getFtpPort();
String?userName?=?kmConfig.getFtpUser();
String?passWord?=?kmConfig.getFtpPassword();
String?path?=?kmConfig.getFtpPath();
FtpClient?ftpClient?=?new?FtpClient(ftpHost,?port);//?ftpHost為FTP服務(wù)器的IP地址,port為FTP服務(wù)器的登陸端口,ftpHost為String型,port為int型。
ftpClient.login(userName,?passWord);//?userName、passWord分別為FTP服務(wù)器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);//?path為FTP服務(wù)器上保存上傳文件的路徑。
try?{
TelnetInputStream?in?=?ftpClient.get(fileName);
byte[]?bytes?=?new?byte[1024];
int?cnt=0;
while?((cnt=in.read(bytes,0,bytes.length))?!=?-1)?{
outputStream.write(bytes,?0,?cnt);
}
//##############################################
//這里文件就已經(jīng)下載完了,自己理解一下
//#############################################
outputStream.close();
in.close();
}?catch?(Exception?e)?{
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
}?catch?(Exception?e)?{
System.out.println("下載文件失??!請檢查系統(tǒng)FTP設(shè)置,并確認(rèn)FTP服務(wù)啟動");
}
}
在jsp/servlet中斷點(diǎn)/多線程下載文件
%@ page import="java.io.File" %%@ page import="java.io.IOException" %%@ page import="java.io.OutputStream" %%@ page import="java.io.RandomAccessFile" %%! public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()); response.setHeader("Server", ""); response.setHeader("Accept-Ranges", "bytes"); long pos = 0; long len; len = raf.length(); if (request.getHeader("Range") != null) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); pos = Long.parseLong(request.getHeader("Range") .replaceAll("bytes=", "") .replaceAll("-", "") ); } response.setHeader("Content-Length", Long.toString(len - pos)); if (pos != 0) { response.setHeader("Content-Range", new StringBuffer() .append("bytes ") .append(pos) .append("-") .append(Long.toString(len - 1)) .append("/") .append(len) .toString() ); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", new StringBuffer() .append("attachment;filename=\"") .append(file.getName()) .append("\"").toString()); raf.seek(pos); byte[] b = new byte[2048]; int i; OutputStream outs = response.getOutputStream(); while ((i = raf.read(b)) != -1) { outs.write(b, 0, i); } raf.close(); fis.close(); }%% String filePath = request.getParameter("file"); filePath = application.getRealPath(filePath); File file = new File(filePath); downloadFile(request, response, file);%
是否可以解決您的問題?