[求助]如何上传和下载图片?
我想做一个校友录.不知道怎么实现向班级像册里上传照片,哪位高手能指点下,谢谢啦!----------------解决方案--------------------------------------------------------
test.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<body bgcolor=dbfdfd text=3f3ab3 >
<% String str=response.encodeURL("acceptFile.jsp");
%>
<P><font size=5><center><b>选择要上传的文件:</font><BR>
<FORM action="<%=str %>" method="post" ENCTYPE="multipart/form-data">
<INPUT type=FILE name="boy" size="45">
<BR> <INPUT type="submit" name ="g" value="提交"></center>
</FORM>
</BODY>
</HTML>
----------------解决方案--------------------------------------------------------
acceptFile.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<body bgcolor=dbfdfd text=3f3ab3 >
<%try{ //用客户的session的id建立一个临时文件:
String tempFileName=(String)session.getId();
//建立临时文件f1:
File f1=new File("F:\\test\\defaultroot",tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上传的全部信息存入f1:
InputStream in=request.getInputStream();
byte b[]=new byte[10000];
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+1,secondLine.length()-1);
random.seek(0); //再定位到文件f1的开头。
//获取第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("F:\\test\\defaultroot",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 ee){}
out.print("<b>文件已上传");
%>
<P> 查看上传的图象效果
<%String str=response.encodeURL("showImage.jsp");
%>
<FORM action="<%=str%>">
<Input type=submit value="查看">
</FOrm >
</BODY>
</HTML>
----------------解决方案--------------------------------------------------------
showImage.jsp
<HTML>
<BODY>
<body bgcolor=dbfdfd text=3f3ab3 >
<% String name=(String)session.getAttribute("Name");
if(name==null)
{name="";
}
out.print("<image src=http://localhost:8080/test/"+name);//图片路径为你上传后图片放的路径
%>
</BODY>
</HTML>
----------------解决方案--------------------------------------------------------
上面的是用文件写的
数据库的我还没写
可以用 不过文件的路径也要改成你jsp放文件的路径
----------------解决方案--------------------------------------------------------
你可以是在试着 在网上下一个jspsmart,这个插件 对上传下载dd很好使的
----------------解决方案--------------------------------------------------------
[大男孩]讲的很详细.太感谢了
以下是引用ok326在2007-7-23 19:56:09的发言:
你可以是在试着 在网上下一个jspsmart,这个插件 对上传下载dd很好使的
[ok326] 谢谢啊,我下下来试试
----------------解决方案--------------------------------------------------------
[大男孩]再请教你个问题:你上面说的方法需不需要插件呀?
----------------解决方案--------------------------------------------------------
呵呵 没什么插件 你直接做成文档放到路径下就可以了
----------------解决方案--------------------------------------------------------
哦.知道了.谢谢
----------------解决方案--------------------------------------------------------