求一个jsp实现的上传和下载源代码(有分,在线等) 求一个jsp实现的上传和下载源代码小弟在做毕业论文要用到上传照片和下载个人信息。搞不定了,求救 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一个简单的例子,你自己想一下吧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==''){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 =endPosition;int j=1;while((>=0)&&(j<=6)){--;random.seek();n=random.readByte();if(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>这是一个思路:你自己想一下吧 太复杂了,下一个SmartUpload包,用这个做<%@ page import="com.jspsmart.upload.*" %>SmartUpload su=new SmartUpload();// su.initialize(pageContext); su.setMaxFileSize(1000000000); su.upload(); String[] filePath=new String[su.getFiles().getCount()]; java.util.Date theDate=new java.util.Date(); long fileNamePre=theDate.getTime(); for (int i=0;i<su.getFiles().getCount();i++){ com.jspsmart.upload.File file = su.getFiles().getFile(i); String FILENAME=file.getFileName(); String filedName=file.getFieldName(); if("saveSite".equals(filedName)){ if (!file.isMissing()) { filePath[i]="moviePic/"+fileNamePre+i+FILENAME.substring(FILENAME.indexOf("."),FILENAME.length()); file.saveAs(filePath[i],su.SAVE_VIRTUAL); saveSite=filePath[i]; } } else{ filePath[i]=""; } } String videoName=su.getRequest().getParameter("videoName"); String tems=su.getRequest().getParameter("temS"); 呵呵,楼上是要自己处理上传的文件吗?还是用组件吧.apache的jakarta commons项目里有个jspupload.自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用. 哥哥们~~~求你们了,急用~~把全部jsp源文件和要用到的组件发到 我的是个简单的组件上传例子哦老大 shenpipi(皮皮) ( ) 信誉:100 2006-03-30 20:27:00 得分: 0 呵呵,楼上是要自己处理上传的文件吗?还是用组件吧.apache的jakarta commons项目里有个jspupload.自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用. 谢了,问题解决了一部分,可以上传了但是不能request.getParameter()得到非file类型值,这个问题解决结分,速度了~~ 也用SmartUpload中的SmartUpload.getRequstParameter()就可以了啊。 http://blog.csdn.net/zdsdiablo/archive/2005/06/16/395713.aspx也是用jspsmart的组件 这两个函数的区别是? javaONE是什么啊 有人能给点相关质料么 求教更好的算法 怎样实现字体滚动,用JAVA语言写得? 一个多线程的socket,没有条件多台机器测试 有谁用过C++写的lucene吗? 变来变去!变过去,怎么变回来呢? 一个文件下载的问题 大哥大姐,帮帮忙!!! 请问如何学习corba,再推荐几本好的书,最好是和java有关的 新手请教句柄问题!多谢撒! 如何建立文件,要求设定文件大小
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=='')
{
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 =endPosition;
int j=1;
while((>=0)&&(j<=6))
{
--;
random.seek();
n=random.readByte();
if(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>
这是一个思路:你自己想一下吧
<%@ page import="com.jspsmart.upload.*" %>
SmartUpload su=new SmartUpload();//
su.initialize(pageContext);
su.setMaxFileSize(1000000000);
su.upload(); String[] filePath=new String[su.getFiles().getCount()];
java.util.Date theDate=new java.util.Date();
long fileNamePre=theDate.getTime(); for (int i=0;i<su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i);
String FILENAME=file.getFileName();
String filedName=file.getFieldName();
if("saveSite".equals(filedName)){
if (!file.isMissing()) {
filePath[i]="moviePic/"+fileNamePre+i+FILENAME.substring(FILENAME.indexOf("."),FILENAME.length());
file.saveAs(filePath[i],su.SAVE_VIRTUAL);
saveSite=filePath[i];
}
}
else{
filePath[i]="";
}
}
String videoName=su.getRequest().getParameter("videoName");
String tems=su.getRequest().getParameter("temS");
自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用.
把全部jsp源文件和要用到的组件发到
呵呵,楼上是要自己处理上传的文件吗?还是用组件吧.apache的jakarta commons项目里有个jspupload.
自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用.
但是不能request.getParameter()得到非file类型值,
这个问题解决结分,速度了~~
也是用jspsmart的组件