当前位置:首页 » 文件管理 » 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 02:12:54 浏览:288
宝马远程服务器如何启用 发布:2024-10-08 02:02:57 浏览:390
c语言freadfwrite 发布:2024-10-08 02:01:15 浏览:853
脚本还不简单吗 发布:2024-10-08 01:54:43 浏览:422
安卓手机如何像平板一样横屏 发布:2024-10-08 01:33:26 浏览:509
wapi认证服务器ip 发布:2024-10-08 01:33:24 浏览:506
centos自带python 发布:2024-10-08 00:53:31 浏览:340
android串口调试助手 发布:2024-10-08 00:45:03 浏览:405
sqlserver2008乱码 发布:2024-10-08 00:39:59 浏览:220
华为电脑服务器系统进不去提示 发布:2024-10-08 00:13:42 浏览:491