當前位置:首頁 » 文件管理 » jsp下載ftp文件

jsp下載ftp文件

發布時間: 2022-10-11 15:44:47

ftp中的文件怎麼下載

用瀏覽器,打開FTP伺服器後,登錄,只要當前的帳號有列表和下載的許可權,就能看到文件,在文件上直接拖放到本地,即可下載。

或者在文件上右鍵,選擇目標另存為,也可以下載並保存到當前電腦中。

Ⅱ jsp實現文件的下載

<%@pagelanguage="java" import="java.io.*,java.net.*" contentType="application/x-msdownload" pageEncoding="UTF-8"%><%
//關於文件下載時採用文件流輸出的方式處理:
//加上response.reset(),並且所有的%>後面不要換行,包括最後一個;
String url = request.getParameter("url");
System.out.print(url);
int k = url.lastIndexOf("\\");
String url1=url.substring(k+1,url.length());
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String filedownload = url;
String filedisplay = url1;
filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
out.clear();
out = pageContext.pushBody();
outp.flush();
}
catch(Exception e)
{
System.out.println("Error!");

}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}
%>

Ⅲ JavaScript實現ftp下載,請大神指點

js沒io的,在前端運行的話你放棄這個念頭吧。

Ⅳ jsp頁面如何實現下載文檔

jsp頁面下載文檔是在jsp中有一個a標簽 ,當用戶點擊a標簽的時候下載文件。
一般採用href屬性直接指向一個伺服器地址,只要鏈接的文件存在,就會給出彈出保存對話框.
點擊a標簽 先執行onclick事件,再請求href中指向的地址。
前端jsp:
<a href="#" onclick="javascript:downloadtest('${app.id}')" id="pluginurl" style="color: #83AFE2;text-decoration:underline;"></a>

然後在js中:
function downloadtest(id){
var url = "<%=request.getContextPath()%>/app/download" + "/" + id;
$("#pluginurl").attr("href",url);
}
後台處理下載邏輯的java代碼:

/**
* 下載文件
* @param id appid
* @param response
*/
@RequestMapping(value="/download/{id}")
public void download(@PathVariable String id, HttpServletResponse response){
String filepath = "";
Result result = appService.getAppById(id);
App app = (App) result.getMap().get("app");
if(app == null){
return;
}
filepath = app.getUrl();

File file = new File(filepath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b= new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();

response.setContentType("application/force-download");
String filename = file.getName();
filename = filename.substring(36, filename.length());
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength( (int) file.length( ) );

while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Ⅳ JSP通過超鏈接下載文件

JSP頁面點擊超鏈接彈出文件下載,代碼如下:

<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
//然後
<ahref="<%=basePath%>/upload/aa.doc}"target="_blank">下&nbsp;&nbsp;載</a>

註:<%= basePath %>獲取部署JSP項目的根目錄,/upload/aa.doc/是根目錄>upload>aa.doc文件,根據需求修改即可。

Ⅵ jsp實現點擊超鏈接下載文件

/** *//**
* 實現文件另存功能
*
* @param text
* 文件內容
* @param fileName
* 文件名稱
* @return
*/
protected String renderFile(String text, String fileName)
throws IOException
{
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName);
response.setContentType("application/octet-stream");
response.setCharacterEncoding("GB2312");
response.getWriter().write(text);
response.flushBuffer();
response.getWriter().close();
return null;
}
下載的action:
/** *//**
* 提供下載的方法
* @return
*/
public String down()
{
String dir = getFullPath() + "/upload/file/";
try
{
if (!FileUtils.exists(dir))
{
new File(dir).mkdirs();
}
Random r = new Random(System.currentTimeMillis());
Integer randomInt = r.nextInt();
this.renderFile("test content:" + randomInt,randomInt + ".txt");
}
catch (IOException e)
{
e.printStackTrace();
this.renderText(e.getMessage());
}
return null;
}
頁面鏈接調用:
下載

Ⅶ jsp 如何實現文件上傳和下載功能

1.jsp頁面
<s:form action="fileAction" namespace="/file" method="POST" enctype="multipart/form-data">
<!-- name為後台對應的參數名稱 -->
<s:file name="files" label="file1"></s:file>
<s:file name="files" label="file2"></s:file>
<s:file name="files" label="file3"></s:file>
<s:submit value="提交" id="submitBut"></s:submit>
</s:form>
2.Action
//單個文件上傳可以用 File files,String filesFileName,String filesContentType
//名稱要與jsp中的name相同(三個變數都要生成get,set)
private File[] files;
// 要以File[]變數名開頭
private String[] filesFileName;
// 要以File[]變數名開頭
private String[] filesContentType;

private ServletContext servletContext;

//Action調用的上傳文件方法
public String execute() {
ServletContext servletContext = ServletActionContext.getServletContext();
String dataDir = servletContext.getRealPath("/file/upload");
System.out.println(dataDir);
for (int i = 0; i < files.length; i++) {
File saveFile = new File(dataDir, filesFileName[i]);
files[i].renameTo(saveFile);
}
return "success";
}
3.配置上傳文件臨時文件夾(在struts.xml中配置)
<constant name="struts.multipart.saveDir" value="c:/temp"/>
文件下載
1.下載的url(到Action)
<a href="${pageContext.request.contextPath}/file/fileAction!down.action">下載</a>
2.struts.xml配置
<package name="file" namespace="/file" extends="struts-default">
<action name="fileAction" class="com.struts2.file.FileAction">
<!-- 下載文件配置 -->
<!--type 為 stream 應用 StreamResult 處理-->
<result name="down" type="stream">
<!--
不管實際類型,待下載文件 ContentType 統一指定為 application/octet-stream
默認為 text/plain
-->
<param name="contentType">application/octet-stream</param>
<!--
默認就是 inputStream,它將會指示 StreamResult 通過 inputName 屬性值的 getter 方法,
比如這里就是 getInputStream() 來獲取下載文件的內容,意味著你的 Action 要有這個方法
-->
<param name="inputName">inputStream</param>
<!--
默認為 inline(在線打開),設置為 attachment 將會告訴瀏覽器下載該文件,filename 指定下載文
件保有存時的文件名,若未指定將會是以瀏覽的頁面名作為文件名,如以 download.action 作為文件名,
這里使用的是動態文件名,${fileName}, 它將通過 Action 的 getFileName() 獲得文件名
-->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!-- 輸出時緩沖區的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
3.Action
//Action調用的下載文件方法
public String down() {
return "down";
}

//獲得下載文件的內容,可以直接讀入一個物理文件或從資料庫中獲取內容
public InputStream getInputStream() throws Exception {
String dir = servletContext.getRealPath("/file/upload");
File file = new File(dir, "icon.png");
if (file.exists()) {
//下載文件
return new FileInputStream(file);

//和 Servlet 中不一樣,這里我們不需對輸出的中文轉碼為 ISO8859-1
//將內容(Struts2 文件下載測試)直接寫入文件,下載的文件名必須是文本(txt)類型
//return new ByteArrayInputStream("Struts2 文件下載測試".getBytes());
}
return null;
}

// 對於配置中的 ${fileName}, 獲得下載保存時的文件名
public String getFileName() {
String fileName ="圖標.png";
try {
// 中文文件名也是需要轉碼為 ISO8859-1,否則亂碼
return new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "icon.png";
}
}

Ⅷ 怎麼下載ftp的文件

你要想從一個FTP伺服器下載文件,你首先要有這個伺服器的賬號和密碼,登錄以後才能夠下載。

熱點內容
神武手游什麼隊伍配置最好 發布:2024-10-08 04:19:05 瀏覽:420
seer資料庫 發布:2024-10-08 04:18:47 瀏覽:477
l3緩存分數下降 發布:2024-10-08 04:10:36 瀏覽:433
linux游戲伺服器 發布:2024-10-08 04:04:17 瀏覽:74
有什麼推薦的網游低配置 發布:2024-10-08 03:17:03 瀏覽:36
淘優惠源碼 發布:2024-10-08 03:17:02 瀏覽:780
linux系統製作 發布:2024-10-08 02:47:15 瀏覽:252
4s緩存怎麼清理 發布:2024-10-08 02:46:42 瀏覽:429
蘋果11面容存儲微信密碼 發布:2024-10-08 02:35:58 瀏覽:764
魔獸243腳本 發布:2024-10-08 02:35:12 瀏覽:640