servlet +jsp 怎么实现上传并在页面显示该图片啊?
展开全部
a.jsp//上传页面
<%@ page contentType="text/html;charSet=gb2312" %>
<html>
<form name="adf" action="aa.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="ubfile" size="50" />
<input type="submit" value="提交" />
</form>
、、、、、、、、、、、、、、、、、、、、、
aa.jsp 上传
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page 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;
}
}
%>
<%
String tempFileName=new String("tempFileName1");
File tempFile1=new File("D:/",tempFileName);
FileOutputStream outputFile1=new FileOutputStream(tempFile1);
InputStream fileSource1=request.getInputStream();
byte b[]=new byte[1000];
int n;
while((n=fileSource1.read(b))!=-1)
outputFile1.write(b,0,n);
outputFile1.close();
fileSource1.close();
RandomAccessFile randomFile1=new RandomAccessFile(tempFile1,"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 FileUploadDir=new File("E:/Program Files/Java/Tomcat/webapps/a/","upload");
FileUploadDir.mkdir();
File saveFile1=new File("E:/Program Files/Java/Tomcat/webapps/a/",fileName);
RandomAccessFile randomFile2=new RandomAccessFile(saveFile1,"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();
}
randomFile1.close();
randomFile2.close();
tempFile1.delete();
%>
<%@ page contentType="text/html;charSet=gb2312" %>
<html>
<form name="adf" action="aa.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="ubfile" size="50" />
<input type="submit" value="提交" />
</form>
、、、、、、、、、、、、、、、、、、、、、
aa.jsp 上传
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page 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;
}
}
%>
<%
String tempFileName=new String("tempFileName1");
File tempFile1=new File("D:/",tempFileName);
FileOutputStream outputFile1=new FileOutputStream(tempFile1);
InputStream fileSource1=request.getInputStream();
byte b[]=new byte[1000];
int n;
while((n=fileSource1.read(b))!=-1)
outputFile1.write(b,0,n);
outputFile1.close();
fileSource1.close();
RandomAccessFile randomFile1=new RandomAccessFile(tempFile1,"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 FileUploadDir=new File("E:/Program Files/Java/Tomcat/webapps/a/","upload");
FileUploadDir.mkdir();
File saveFile1=new File("E:/Program Files/Java/Tomcat/webapps/a/",fileName);
RandomAccessFile randomFile2=new RandomAccessFile(saveFile1,"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();
}
randomFile1.close();
randomFile2.close();
tempFile1.delete();
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先要去阿帕奇下两个包commons-io.jar;commons-fileupload.jar两个包,用这两个包写一个servlet用来上传文件,上传的同时把文件名存入数据库,上传后在jsp中用相对路径访问,不懂可HI我。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额~
别转换成二进制了。
存文件,保存地址到数据库吧
别转换成二进制了。
存文件,保存地址到数据库吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上传是上传,不过显示图片,用JS做就行了~~毕竟是在自己电脑上呢~,要不有点浪费
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询