




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
下載jar包.ftp. 代碼如下:上傳:JSP: 文件上傳 文件位置: SERVLET:package com.test;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class UploadServletA extends HttpServlet private static final long serialVersionUID = 1L;public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException tryrequest.setCharacterEncoding(utf-8);response.setCharacterEncoding(UTF-8); response.setContentType(text/html; charset=UTF-8); String myFile = request.getParameter(mfile); /獲取頁面提交的文件int indexOf = myFile.lastIndexOf(); /獲取最后一個“/”所在的索引位String fileName = myFile.substring(indexOf); /獲取文件上傳的文件名/創(chuàng)建輸出流把指定的文件寫入WEB工程的upload目錄下(需要在WebRoot目錄下創(chuàng)建upload目錄)。FileOutputStream fos = new FileOutputStream(d:upload + fileName);/創(chuàng)建輸入流讀取指定文件java.io.FileInputStream fis = new java.io.FileInputStream(new java.io.File(myFile);byte buffer = new byte256;int len = 0;while (len = fis.read(buffer) 0) /循環(huán)讀取文件到輸出流fos.write(buffer, 0, len); /使用輸出流輸出文件。fos.flush();fis.close();fos.close(); PrintWriter pw=response.getWriter(); pw.println(); pw.println(); pw.println(); pw.println(); pw.println(alert(上傳成功!); pw.println(); pw.println(); pw.println(); pw.println();catch(Exception e)e.printStackTrace();PrintWriter pw=response.getWriter(); pw.println(); pw.println(); pw.println(); pw.println(); pw.println(alert(上傳失?。?; pw.println(); pw.println(); pw.println(); pw.println();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request, response);下載JSP: base href= 文件下載 !- 請點擊文件名下載! 中文.txt SERVLET:package com.test;import java.io.File;import java.io.IOException;import .URLEncoder;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import .ftp.FTPClient;import .ftp.FTPClientConfig;import .ftp.FTPFile;import .ftp.FTPReply;/* * * author kfzx-zhouy05 */public class FtpDownloadServlet extends HttpServlet private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding(utf-8);response.setCharacterEncoding(UTF-8); response.setContentType(text/html; charset=UTF-8);String path = upload;File f2 = new File(upload中文.txt);String filename2 = f2.getName();ServletOutputStream outputStream = response.getOutputStream();downFile(6, 21, zhouyun0243, zhouyun0243, path,filename2, outputStream, response);public void init() throws ServletException public static void downFile(String ip, int port, String username,String password, String remotePath, String fileName,ServletOutputStream outputStream, HttpServletResponse response) FTPClient ftp = new FTPClient();int count = 0;try int reply;ftp.connect(ip, port);/ 下面三行代碼必須要,而且不能改變編碼格式 ftp.setControlEncoding(UTF-8);FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);conf.setServerLanguageCode(zh);/ 如果采用默認端口,可以使用ftp.connect(url) 的方式直接連接FTP服務(wù)器 ftp.login(username, password);/ 登錄 ftp.setFileType(FTPClient.BINARY_FILE_TYPE);reply = ftp.getReplyCode();if (FTPReply.isPositiveCompletion(reply) System.out.println(連接服務(wù)器失敗);outputStream.println();outputStream.println();outputStream.println(alert(Connect server fail!);outputStream.println(document.location.href=http:/localhost:8080/struts2HibernateSpring/download.jsp);outputStream.println();outputStream.println();outputStream.close();ftp.disconnect();return;System.out.println(登陸成功。);ftp.changeWorkingDirectory(remotePath);/ 轉(zhuǎn)移到FTP服務(wù)器目錄 FTPFile fs = ftp.listFiles(); / 得到目錄的相應(yīng)文件列表 for (int i = 0; i fs.length; i+) FTPFile ff = fsi;if (ff.getName().equals(fileName) count+;String filename = fileName;/ 這個就就是彈出下載對話框的關(guān)鍵代碼 response.setHeader(Content-disposition,attachment;filename=+ URLEncoder.encode(filename, UTF-8);/ 將文件保存到輸出流outputStream中 ftp.retrieveFile(new String(ff.getName().getBytes(UTF-8),ISO-8859-1), outputStream);outputStream.flush();if (count = 1) /下載成功outputStream.close();ftp.logout();ftp.disconnect(); else /文件找不到outputStream.println();outputStream.println();outputStream.println(alert(File is not exist!);outputStream.println(document.location.href=http:/localhost:8080/struts2HibernateSpring/download.jsp);outputStream.println();outputStream.println();outputStream.close();ftp.logout();ftp.disconnect(); catch (Exception e) e.printStackTrace();try outputStream.println();outputStream.println();outputStream.println(alert(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 影視道具專業(yè)保險及責任承擔協(xié)議
- 知識產(chǎn)權(quán)代管與新能源技術(shù)合作合同
- 微信視頻號美食烘焙教程制作與推廣服務(wù)協(xié)議
- 智能家居股權(quán)變更登記及智能家居生態(tài)合作協(xié)議
- 建筑空調(diào)系統(tǒng)安裝與節(jié)能改造及運維服務(wù)協(xié)議
- 固廢處理與資源化利用項目投資協(xié)議
- 商業(yè)銀行柜員崗位全職聘用與業(yè)務(wù)流程優(yōu)化合同
- 智能教育平臺課程內(nèi)容版權(quán)授權(quán)與品牌合作合同
- 數(shù)據(jù)中心備用電源租賃及智能運維服務(wù)合同
- 抖音平臺火花話題挑戰(zhàn)賽合作伙伴市場推廣與品牌宣傳協(xié)議
- 新聞記者職業(yè)資格《新聞基礎(chǔ)知識》考試題庫(含答案)
- 國開2024年秋《心理健康教育》形考任務(wù)1-9答案
- 妊娠紋的治療
- 電力運維管理平臺方案設(shè)計
- 安全培訓(xùn)管理體系
- 古詩教案模板范文
- 屠宰場安全培訓(xùn)
- 光伏電站運維課件
- 廠區(qū)綠化環(huán)境提升方案
- 南京工業(yè)大學(xué)《化工廢水處理》2022-2023學(xué)年第一學(xué)期期末試卷
- 高三第二輪復(fù)習之文言翻譯(李麗君)省公開課獲獎?wù)n件市賽課比賽一等獎?wù)n件
評論
0/150
提交評論