| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 817 人关注过本帖
标题:文件上传下载代码
只看楼主 加入收藏
xiao_2008
Rank: 1
等 级:新手上路
帖 子:103
专家分:0
注 册:2006-4-22
收藏
 问题点数:0 回复次数:3 
文件上传下载代码
各位大侠们请教下
文件的文件上传下载代码,感激不尽!
struts ,mvc,JSP都可以!
搜索更多相关主题的帖子: 文件 代码 
2006-04-25 08:21
xieyanyong00
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-3-24
收藏
得分:0 

<%@ page contentType="text/html;charset=gb2312" import="java.io.*" %>
<%! public String codeToString(String str)
{
String s=str;
try{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e){return s;}
} %>
<html>
<body>
<%try{
String tempFileName=new String("tempFileName1");
File tempFile=new File("D:/",tempFileName);
FileOutputStream output=new FileOutputStream(tempFile);
InputStream in=request.getInputStream();
byte b[]=new byte[1000];
int n;
while((n=in.read(b))!=-1)
output.write(b,0,n);
output.close();
in.close();
RandomAccessFile randomFile1=new RandomAccessFile(tempFile,"r");
randomFile1.readLine();
String FilePath=randomFile1.readLine();
int Position=FilePath.lastIndexOf('\\');
String FileName=codeToString(FilePath.substring(Position+1,FilePath.length()-1));
randomFile1.seek(0);
long forthEnterPosition=0;
int forth=1;
while((n=randomFile1.readByte())!=-1&&forth<=4)
if(n=='\n')
{forthEnterPosition=randomFile1.getFilePointer();
forth++;}
File uploadDir=new File("D:/","upload");
uploadDir.mkdir();
File saveFile=new File("D:/upload/",FileName);
RandomAccessFile randomFile2=new RandomAccessFile(saveFile,"rw");
randomFile1.seek(randomFile1.length());
long endPosition=randomFile1.getFilePointer();
int j=1;
while((endPosition>=0)&&j<=4)
{
endPosition--;
randomFile1.seek(endPosition);
if(randomFile1.readByte()=='\n')
j++;
}
randomFile1.seek(forthEnterPosition);
long startPoint=randomFile1.getFilePointer();
while(startPoint<endPosition-1)
{
randomFile2.write(randomFile1.readByte());
startPoint=randomFile1.getFilePointer();
}

randomFile2.close();
randomFile1.close();
out.print("文件"+FileName+"上传成功!");

}
catch(Exception e)
{e.printStackTrace();}
%>
</body>
</html>

2006-04-30 17:08
dreame
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:521
专家分:0
注 册:2005-10-23
收藏
得分:0 
建议利用组建,uploadbean上传功能很强大,还有apache的fileupload也不错!http://www.

有时候真的想放弃寂寞的等待,但她是那么的可爱!I love u forever!
2006-04-30 22:53
xiao_2008
Rank: 1
等 级:新手上路
帖 子:103
专家分:0
注 册:2006-4-22
收藏
得分:0 

我也感觉版主说得没有错
我就用了那个fileupload 不错
可以上传多个文件
我用struts做的只能够上传图片的几中图形
还要好好研究下


看动画片
2006-05-08 08:58
快速回复:文件上传下载代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014746 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved