社区
Web 开发
帖子详情
如何用jsp实现文件上传和下载,见天加班,就为这个问题,等待中....
heroes3player
2003-10-19 10:13:15
给个例子先
...全文
1044
10
打赏
收藏
如何用jsp实现文件上传和下载,见天加班,就为这个问题,等待中....
给个例子先
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
heroes3player
2003-10-19
打赏
举报
回复
谢谢大家了,有这些提示,对我应该是足够了,马上揭贴。
heroes3player
2003-10-19
打赏
举报
回复
关于用第三方组件的方法,我已经找到了。
最好是自己写
wind666
2003-10-19
打赏
举报
回复
这是用文件来的,可以用数据库的
wind666
2003-10-19
打赏
举报
回复
给你一个简单的例子,你自己想一下吧
loadfile.html
<%@page contenttype="text/html;charset=gb2312"%>
<html>
<body>
<p>单击超连接下载zip文档book.zip
<br>
<a href="loadfile.jsp">下载book.zip
</body>
</html>
loadfile.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@page import="java.io.*"%>
<html>
<body>
<%
OutputStream o=response.getOutputStream();
byte b[]=new byte[500];
File fileLoad=new File("E:/aa.mdb");
response.setHeader("contnt-disposition","attachment; filename="+"aa.rar");
response.setContentType("application/x-tar");
long fileLength=fileLoad.length();
String length1=String.valueOf(fileLength);
response.setHeader("Content_Length",length1);
FileInputStream in=new FileInputStream(fileLoad);
int n;
while((n=in.read(b))!=-1)
{
o.write(b,0,n);
}
%>
</body>
</html>
upload.html
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<p>选择要上传的文件:<br>
<form action="upload.jsp" method=post ENCTYPE="multipart/form-data">
<input type="FILE" name="boy" size="38">
<br>
<input type="submit" name="g" value="提交">
</body>
</html>
upload.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<body>
<% try{//用户的seesion 的id 建立一个临时文件。
String tempFileName=(String)session.getId();
//建立临时文件f1.
File f1=new File("E:/",tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上全部信息存入f1.
InputStream in=request.getInputStream();
byte b[]=new byte[1000];
int n;
while((n=in.read(b))!=-1)
{
o.write(b,0,n);
}
o.close();in.close();
//读取临时文件f1,从中获取上传文件的名字和上传的内容。
RandomAccessFile random=new RandomAccessFile(f1,"r");
//读取f1 的第2行,析取上传文件的名字。
int second=-1;
String secondLine=null;
while(second<=2)
{secondLine=random.readLine();
second++;
}
//获取第2行中'\\'最后出现的位置。
int position =secondLine.lastIndexOf('\\');
String fileName=secondLine.substring(position,secondLine.length()-1);
random.seek(0);//再定位到文件的开头。
//获取文件的第4 行的回车符号的位置。
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth!=4))
{
if(n=='\n')
{
forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件名字,将该文件存入磁盘。
File f2=new File("E:/",fileName);
session.setAttribute("Name",fileName);//供showImage.jsp页面使用
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定文件f1中包含客户上传的文件 的内容的最后位置,即倒数第6行。
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{
mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{
endPosition=random.getFilePointer();
j++;
}
}
//将random流指向文件f1的第4 行结束的位置。
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1 读出客房上文件 并存入f2(读取从第4 行结束位置和倒数第6行之间的内容)。
while(startPoint<endPosition-1)
{
n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
f1.delete();//删除临时文件。
}
catch(IOException e){}
out.print("文件已上传");
%>
</body>
</html>
这是一个思路:你自己想一下吧
希偌
2003-10-19
打赏
举报
回复
http://www.csdn.net/develop/Read_Article.asp?Id=16432
hajavaor
2003-10-19
打赏
举报
回复
我自己写的文件上传下载程序。
一个邮件系统附件的上传与下载。
也花了我好长时间。
可以与我联系:QQ:19843788
我们可以讨论讨论。
先给分再联系。
lzy9819
2003-10-19
打赏
举报
回复
JSP利用组件实现文件上传的全攻略
一、首先下载jspsmartupload组件
http://www.jspsmart.com 或
http://www.hoodle.net/other/zujian/jspSmartUpload.zip
二、将目录jspsmartupload/wib_inf/classes中的内容拷贝到网站所在的实际目录中的web_inf中(resin是这个目录,其他的可能是classes,具体请查阅jspsmartupload/help/setup.htm)
三、如果是resin运行JSP,请在resin的conf/resin.conf中的
<web-app>和</web-app>中加入:
<path-mapping url-pattern='/upload/*' real-path='f:\jsp\jspsmartupload\upload'/>
四、上传界面的代码如下:(文件名:insert.htm)
<FORM METHOD="POST" ACTION=" uploadfile.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE2" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE3" SIZE="50"><BR>
<INPUT TYPE="FILE" NAME="FILE4" SIZE="50"><BR>
主题:<input type="text" name="text1" ><br>
<INPUT type=submit value=写 完 name=ok>
</form>
注意上面的real-path目录
五、uploadfile.jsp的代码如下:
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
<BODY BGCOLOR="white">
<H1>hoodle-jsp</H1>
<HR>
<%
int count=0;
mySmartUpload.initialize(pageContext);
'文件上传
mySmartUpload.upload();
'获得文本的内容
String content = mySmartUpload.getRequest().getParameter("text1");
'显示文本的内容
out.println(content);
'上传的情况统计
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
myFile.saveAs("/upload/" + myFile.getFileName());
out.println("FieldName = " + myFile.getFieldName() + "<BR>");
out.println("Size = " + myFile.getSize() + "<BR>");
out.println("FileName = " + myFile.getFileName() + "<BR>");
out.println("FileExt = " + myFile.getFileExt() + "<BR>");
out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
out.println("ContentType = " + myFile.getContentType() + "<BR>");
out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
count ++;
}
}
out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>");
out.println(count + " file(s) uploaded.");
%>
</BODY>
</HTML>
heroes3player
2003-10-19
打赏
举报
回复
我现在就是在搜索呀,手边的两本参考书没有提级这方面的问题。CSDN好象不提供关键字的检索,我也不可能把所有的帖子一个一个的找一遍呀。
老土豆T
2003-10-19
打赏
举报
回复
参见FAQ..或者搜索上传,smartupload ,下载.. 或者google搜索 (搜索的好处能发现好多关联的问题。)
论坛是发现问题,解决问题,讨论问题的地方,你的问题大家都讨论过了,所以
如果论坛上总是出现相同的问题,可能对一些常来的人失去兴趣了,不利于论坛健康发展。而csdn所谓的人气旺盛,大部分也都是我们这些初学者,
即使我贴了,对你的帮助也不大。。 关于提问参见(http://forum.hibernate.org.cn/viewtopic.php?t=70&sid=bb5a01c054993b7a2ee132dbe93d4aeb)
btw,我也好多问题不懂,一般都是搜索的。。只有知己明白,了解又有疑惑的问题才拿来提问,提问也是个讨论的过程。 希望你能理解,谢谢:)
heroes3player
2003-10-19
打赏
举报
回复
没人回答吗
自动驾驶运动规划(Motion Planning).pdf
自动驾驶运动规划(Motion Planning)
问题
分析
财务数据分析模型6.xlsx
Excel数据看板,Excel办公模板,Excel模板
下载
,Excel数据统计,数据展示
人力资源数据分析看版.xlsx
Excel数据看板,Excel办公模板,Excel模板
下载
,Excel数据统计,数据展示
重庆大学2011-2012(2)数字电子技术II.pdf
重庆大学期末考试试卷,重大期末考试试题,试题及答案
随波逐流CTF编码工具 V5.6 20240424.rar
由随波逐编写开发,CTF编码工具为用户提供丰富的离线加密解密功能,还可以对字符编码进行转换,文件隐写查看,用户可以根据自己的需求来使用功能,非常实用,能够提高大家的工作效率!
Web 开发
81,092
社区成员
341,716
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章