站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Socket做网页上传文件...无法得到结束标志

Socket做网页上传文件...无法得到结束标志

摘要: Socket做网页上传文件...无法得到结束标志


POST /mywebftp/upfile.class HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://127.0.0.1/mywebftp/upfile.class
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7d636b2430144
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)
Host: 127.0.0.1
Content-Length: 563855
Connection: Keep-Alive
Cache-Control: no-cache

-----------------------------7d636b2430144
Content-Disposition: form-data; name="file"; filename="C:/Documents and Settings/new/桌面/a.txt"
Content-Type: image/jpeg

...
...(二进制文件)
-----------------------------7d636b2430144
Content-Disposition: form-data; name="OK"

确定上传
-----------------------------7d636b2430144--

//上面是http请求的信息...
-----------------------------7d636b2430144这个是用来标志结束的..
但在读取时...怎么知道 读到有这个标志呢?
我用的方法是:
byte[] buff=new byte[1024*1024*2];
int num=in.read(buff);
while(num!=-1)
{
int e=buff.toString().indexOf(endstr);
if(e!=-1)
{
fout.write(buff,0,e);
break;
}
fout.write(buff,0,num);
num=in.read(buff);
}
这个方法明显不行!!!
请问大家有什么 别的方法没有!!
谢谢!!!
endstr 就是上面的:-----------------------------7d636b2430144

谢谢!!



bean是从网上下载现成的,如下:
package kingstar_www.chcn.bbs;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

public class UpLoadFile
{
public UpLoadFile()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}
private String getFileName(String s)
{
int i = s.lastIndexOf("//");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

public void setUploadDirectory(String s)
{
uploadDirectory = s;
}

public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}

public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}

public void uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
uploadFile(httpservletrequest.getInputStream());
}

public void uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf("/"")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("/""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}

private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
try
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
}
catch(IOException ioexception)
{
return null;
}
try
{
if(s == null)
return new String(abyte0, 0, ai[0]);
else
return new String(abyte0, 0, ai[0], s);
}
catch(Exception exception)
{
return null;
}
}
private static String newline = "/n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;

}



我就是不想用servlet才自己写的...用它..我还问....!


↑返回目录
前一篇: 怎么用正则表达式提取-lt
后一篇: 急。急急。。请各位大人帮帮我啊。页面重定向不到我写的地址小弟穷分少。多谢