The read() methd didn't return -1 except disk error, network error etc.So first thing, you should get the file's size. And the second, you should compare it with the data size which has been read from file. Last, if one value is equal to another, then quit loop.
我前面提到的方法你試了嗎,在我機子上都可以的 try{ FileInputStream fi=new FileInputStream("d:\\MasterEjb\\testJList.java"); //int len = fi.getContentLength(); //System.out.println(len); byte[] buffer = new byte[1024]; InputStream in = new DataInputStream(fi); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int tempLen =0; while((tempLen = in.read(buffer))!= -1) { System.out.println(tempLen); bout.write(buffer,0,tempLen); } System.out.println("out while"); }catch(Exception e){ }
lifeiqhp(飛哥) :你的方法不适用于network。 menlion(浪·星魂):it should be right when u operate on the net stream.But it'll return the value "-1" when the program reach the end of the file on the disk. 我最后改了while循环: while(bout.size() < len) { tempLen = in.read(buffer,0,buffer.length); bout.write(buffer,0,tempLen); }
改為:
tempLen = in.read(buffer)) != -1
tempLen = in.read(buffer,0,buffer.length)
把每次读取的长度赋予tempLen,如果读取结束,tempLen被赋予-1
楼主的程序好像没有错啊?
import java.io.*;import javax.servlet.*;
import javax.servlet.http.*;public class Hello extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
System.out.println("execute");
int len = request.getContentLength();
System.out.println(len);
DataInputStream in = new DataInputStream(request.getInputStream());
ByteArrayOutputStream bout = new ByteArrayOutputStream(); int tempLen =0;
byte[] buffer = new byte[1024];
while((tempLen = in.read(buffer,0,buffer.length)) != -1) {
System.out.println(tempLen);
bout.write(buffer,0,tempLen);
}
bout.flush();
System.out.println("out while"); String[] tokens={"name=\"","\"; filename=\"", "\"\r\n","Content-Type: ","\r\n\r\n"};
String gs = "-----------------------------7d22f7261e00dc";
int count = 0;
System.out.println(request.getContentType());
/*OutputStream outt=new BufferedOutputStream(
new FileOutputStream("Receive.log",true));
byte[] breaker="\r\nNewLog: -------------------->\r\n".getBytes();
System.out.println(request.getContentType());
outt.write(breaker,0,breaker.length);
outt.write(buffer);
outt.close();*/ //--------------------------------
response.setContentType("text/html");
PrintWriter out = response.getWriter(); out.println("<html>");
out.println("<head>"); out.println("</head>");
out.println("<body>");
out.println("hello world!");
out.println("</body>");
out.println("</html>");
}
}
And the second, you should compare it with the data size which has been read from file.
Last, if one value is equal to another, then quit loop.
try{
FileInputStream fi=new FileInputStream("d:\\MasterEjb\\testJList.java");
//int len = fi.getContentLength();
//System.out.println(len);
byte[] buffer = new byte[1024];
InputStream in = new DataInputStream(fi);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int tempLen =0;
while((tempLen = in.read(buffer))!= -1) {
System.out.println(tempLen);
bout.write(buffer,0,tempLen);
}
System.out.println("out while");
}catch(Exception e){
}
menlion(浪·星魂):it should be right when u operate on the net stream.But it'll return the value "-1" when the program reach the end of the file on the disk.
我最后改了while循环:
while(bout.size() < len) {
tempLen = in.read(buffer,0,buffer.length);
bout.write(buffer,0,tempLen);
}