You limited it to 10 by your while loop using 10. Since stream.read()
returns -1 at the end of the stream, use that in the while loop as the control:
int count = 0;
int b = 0;
while ((b=stream.read()) !=-1)
{
fos.write(b);
count++;
}
문제
I am working on a Web Server, I am stuck on the HTTP method PUT ... I currently can only bet 10 bytes of data from the client when he tries to upload a file, bellow is what I have so far.
InputStream stream = connection.getInputStream();
OutputStream fos = Files.newOutputStream(path);
int count = 0;
while (count < 10) {
int b = stream.read();
if (b == -1) break;
fos.write(b);
++count;
}
fos.close();
Please tell me how I can get the as much input the client writes.
해결책
You limited it to 10 by your while loop using 10. Since stream.read()
returns -1 at the end of the stream, use that in the while loop as the control:
int count = 0;
int b = 0;
while ((b=stream.read()) !=-1)
{
fos.write(b);
count++;
}
다른 팁
public void receiveFile(InputStream is){
//Set a really big filesize
int filesize = 6022386;
int bytesRead;
int current = 0;
byte[] mybytearray = new byte[filesize];
try(FileOutputStream fos = new FileOutputStream("fileReceived.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos)){
//Read till you get a -1 returned by is.read(....)
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current,
(mybytearray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
bos.close();
}
catch (FileNotFoundException fnfe){
System.err.println("File not found.");
}
catch (SecurityException se){
System.err.println("A Security Issue Occurred.");
}
}
Based on this one: FTP client server model for file transfer in Java