请教Java Jsp中Ftp文件下载问题
在网上看了些Ftp文件下载的,下载是实现了,但是保存的路径是写死的.如何让用户点击"下载"时弹出一个选择路径的提示框?我action中该如何写?望各位赐教....
在网上看了些Ftp文件下载的,下载是实现了,但是保存的路径是写死的.如何让用户点击"下载"时弹出一个选择路径的提示框?我action中该如何写? 望各位赐教.
展开
2个回答
展开全部
<%@ page contentType="text/html; charset=GBK"%>
<%@ page language="java" import="java.io.IOException" pageEncoding="GBK"%>
<%@ page import="com.enterprisedt.net.ftp.FTPClient"%>
<%@ page import="com.enterprisedt.net.ftp.FTPTransferType"%>
<%@ page import="java.io.*,com.enterprisedt.net.ftp.FTPConnectMode;"%>
<html>
<head>
<title>主页面</title>
</head>
<body>
<%
String path = null;
String filename = null;
path = request.getParameter("path");
filename = request.getParameter("filename");
//response.setContentType("application/unknown");//设置为下载application/x-download
//其中%20是空格在UTF-8下的编码
//filename = URLEncoder.encode(filename, "UTF-8");
//filename = new String(filename.getBytes("gb2312"),"ISO8859-1");
//response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
String host = "192.168.12.49";
String users = "admin";
String passwords = "admin";
if (path.trim().length() > 1) {
path = path.trim() + "/";
}
String remoteFile = path + filename; //确定源文件的位置
String downFile = "d:/" + filename; //确定源文件的位置
System.out.println(remoteFile + " 正在下载中,请稍等................");
try {
OutputStream outputStream = response.getOutputStream();
FTPClient client = new FTPClient();
client.setRemoteHost(host);
//client.setDetectTransferMode(true);
client.connect();
client.login(users, passwords);
client.setConnectMode(FTPConnectMode.ACTIVE);
client.setType(FTPTransferType.BINARY);
//client.get(outputStream, remoteFile); //*方式一:将ftp上的文件取出后,写入到response(outputStream),以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本
//*一直存在中文文件名没有解决的问题
client.get(downFile, remoteFile); //*方式二:将FTP上文件取出后,直接下载到D盘下
outputStream.close();
client.quit();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>
<%@ page language="java" import="java.io.IOException" pageEncoding="GBK"%>
<%@ page import="com.enterprisedt.net.ftp.FTPClient"%>
<%@ page import="com.enterprisedt.net.ftp.FTPTransferType"%>
<%@ page import="java.io.*,com.enterprisedt.net.ftp.FTPConnectMode;"%>
<html>
<head>
<title>主页面</title>
</head>
<body>
<%
String path = null;
String filename = null;
path = request.getParameter("path");
filename = request.getParameter("filename");
//response.setContentType("application/unknown");//设置为下载application/x-download
//其中%20是空格在UTF-8下的编码
//filename = URLEncoder.encode(filename, "UTF-8");
//filename = new String(filename.getBytes("gb2312"),"ISO8859-1");
//response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
String host = "192.168.12.49";
String users = "admin";
String passwords = "admin";
if (path.trim().length() > 1) {
path = path.trim() + "/";
}
String remoteFile = path + filename; //确定源文件的位置
String downFile = "d:/" + filename; //确定源文件的位置
System.out.println(remoteFile + " 正在下载中,请稍等................");
try {
OutputStream outputStream = response.getOutputStream();
FTPClient client = new FTPClient();
client.setRemoteHost(host);
//client.setDetectTransferMode(true);
client.connect();
client.login(users, passwords);
client.setConnectMode(FTPConnectMode.ACTIVE);
client.setType(FTPTransferType.BINARY);
//client.get(outputStream, remoteFile); //*方式一:将ftp上的文件取出后,写入到response(outputStream),以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本
//*一直存在中文文件名没有解决的问题
client.get(downFile, remoteFile); //*方式二:将FTP上文件取出后,直接下载到D盘下
outputStream.close();
client.quit();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询