服务端如何读取客户端的文件?

梦断酒醒 2003-01-17 12:28:38
在客户端用<input type="file" name="file">可以选择一个本地文件,但向服务端提交后,我理解服务端取得的只是文件的路径,我的理解是不否正确?如果正确,在服务端如何取得这个文件的内容呢?
...全文
225 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
redjava 2003-01-20
  • 打赏
  • 举报
回复
whodsow(whodsow) 你好 能传多大的文件?
我试过 几M没问题 几百M不行
不用fpt 有什么办法能传几百M?
pzs8888 2003-01-20
  • 打赏
  • 举报
回复
gz
jspxnet 2003-01-18
  • 打赏
  • 举报
回复
不行~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
除非用ftp
caoze 2003-01-17
  • 打赏
  • 举报
回复
jspsmartuploade仅是一个工具罢,它也是在其它的基础上开发出的。

只要form中enctype="multipart/form-data" 与method="post"

在servlet中的dopost(request, response)中就可以处理这个文件的。

当然,用别人包好的东东来实现当然要方便很多。
liucm 2003-01-17
  • 打赏
  • 举报
回复
服务端要想取客户端的文件,只有通过上传
jspsmartuploade可以上传
bdsc 2003-01-17
  • 打赏
  • 举报
回复
服务器取到的是文本(很长的字符串)(文件的内容)
dou1204 2003-01-17
  • 打赏
  • 举报
回复
我也碰到这问题,和你的理解一样,下个JSPSMARTUPLOAD来用吧
whodsow 2003-01-17
  • 打赏
  • 举报
回复
注:上贴中D:\Documents and Settings\Administrator\My Documents\?·.txt后面的乱码是中文字符。
whodsow 2003-01-17
  • 打赏
  • 举报
回复
有,这是我编的一个客户端上传文件的JSP。
upLoad.htm
----------------------------------------
<form method="POST" action="FileLoad.jsp" name="fileForm" enctype="multipart/form-data">
<input type="file" name="file" size="20">
<input type="submit" value="上传" name="submit">
</form>
FileLoad.jsp
--------------------------------------
<%@ page language="java" import="java.io.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<%
String filename=null;
String path=request.getRealPath("/");
String contentType=request.getContentType();
int boundaryIndex=contentType.indexOf("boundary=");
String boundary=contentType.substring(boundaryIndex+9);
int boundaryStrLength=boundary.length();

ServletInputStream servIn=request.getInputStream();

DataInputStream in=new DataInputStream(servIn);

String line;
while((line=in.readLine())!=null)
{
int index=line.indexOf("filename=");
if(index!=-1)
{
int indexsub=line.lastIndexOf("\\");
String temp=line.substring(indexsub+1);
int len=temp.length();
filename=temp.substring(0,len-1);
}
//getServletContext().log("Got line:"+line);
if(line.trim().length()==0)
break;
}

ByteArrayOutputStream byteOut=new ByteArrayOutputStream(request.getContentLength());
byte []buffer=new byte[4096];
int len;

while((len=in.read(buffer))>0)
{
byteOut.write(buffer,0,len);
}
byte []outBytes=byteOut.toByteArray();

//in.close();
ByteArrayInputStream bai=new ByteArrayInputStream(outBytes);

int seek=256;
if(outBytes.length<seek)
{
seek=outBytes.length;
}
byte []byteBuffer=new byte[seek];
bai.skip((long)(outBytes.length-seek));
bai.read(byteBuffer);
bai.close();
String show="";
show=new String(byteBuffer);
int index=boundary.lastIndexOf('-');
line=boundary.substring(0,index+1);
line=line+"--";
index=show.lastIndexOf(line);
int indexEnd=show.lastIndexOf("--");
String lastBoundary=show.substring(index,indexEnd);
index=show.indexOf(lastBoundary);
line=show.substring(index-2);
byte []byteDelete=line.getBytes();

if(filename==null)
filename=new String("file");
else
{
byte []tmp=filename.getBytes("ISO8859_1");
filename=new String(tmp);
}
FileOutputStream fileOut=new FileOutputStream(path+filename);
//报头附的字符:"-----------------------------7d32d03a9016a\r\nContent-Disposition: form-data; name="submit"\r\n\r\n????\r\n-----------------------------7d32d03a9016a--\r\n"
//out.println(outBytes.length+"=|"+new String(outBytes)+"|");
//out.println(byteDelete.length+"=|"+new String(byteDelete)+"|");
fileOut.write(outBytes,0,outBytes.length-byteDelete.length);
fileOut.close();
%>
<H3>文件<b><font color="#ff0000"><%=filename%></font></b>上传成功,上传<b><font color="#0000ff"><%=outBytes.length-byteDelete.length%></font></b>字节。
资料:文件上传时对80端口监听所得数据。如下,其中(\r\n)代表空白行。
POST / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7d32c12b9016a
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.0)
Host: localhost:1024
Content-Length: 684
Connection: Keep-Alive
Cache-Control: no-cache
(\r\n)
-----------------------------7d32c12b9016a
Content-Disposition: form-data; name="file"; filename="D:\Documents and Settings\Administrator\My Documents\?·.txt"
Content-Type: text/plain
(\r\n)
file content,file content,file content,file content
-----------------------------7d32c12b9016a
Content-Disposition: form-data; name="submit"

上传
-----------------------------7d32c12b9016a--
(\r\n)
你自己结合起来分析一下吧。



凋零的老树 2003-01-17
  • 打赏
  • 举报
回复
直接读取???不能吧,如果能的话,你上网就很危险了,人家不知不觉的读到你硬盘的上的信息了
hanty 2003-01-17
  • 打赏
  • 举报
回复
是根据连文件名称带路径的全称进行的文件序列化串
hanty 2003-01-17
  • 打赏
  • 举报
回复
上传后进行读取
没有其他办法直接读取
dou1204 2003-01-17
  • 打赏
  • 举报
回复
是,但是还是现成的方便
maxpain 2003-01-17
  • 打赏
  • 举报
回复
你就是要实现一个上传的功能,可以用工具,也可以自己写一个。

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧