2013年11月19日星期二

Will read the data from the stream into a byte array

deposit How will the servlet request.getInputStream () in the input stream data saved to a byte array , with a

InputStream in = request.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);

failure , in.available () debug value 0
------ Solution ----------------------- ---------------------

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

------ For reference only ----------- ----------------------------

request.getContentLength () to obtain a value of -1 , the document does is pass up , because I test

// FileOutputStream fos = new FileOutputStream("F://123.jpg");
// int len = 0;
// byte[] bytes = new byte[1024];
// while((len = in.read(bytes))!=-1){
// fos.write(bytes, 0, len);
// fos.flush();
// }
// fos.close();

indeed write down the file , but because the demand need to write a byte array , it is not written into
------ For reference only ---------- -----------------------------

request.getContentLength () to obtain a value of -1 , file indeed pass up , because I test   
  

// FileOutputStream fos = new FileOutputStream("F://123.jpg");
// int len = 0;
// byte[] bytes = new byte[1024];
// while((len = in.read(bytes))!=-1){
// fos.write(bytes, 0, len);
// fos.flush();
// }
// fos.close();
  
indeed write down the file , but because the demand need to write a byte array , it is not written into  

You do not upload it ?
form enctype = "multipart / form-data" is set up ?
is a post request it ?
If yes get request length is -1
------ For reference only -------------------------- -------------

request.getContentLength () to obtain a value of -1 , the file is indeed pass up , because I test     
    

// FileOutputStream fos = new FileOutputStream("F://123.jpg");
// int len = 0;
// byte[] bytes = new byte[1024];
// while((len = in.read(bytes))!=-1){
// fos.write(bytes, 0, len);
// fos.flush();
// }
// fos.close();
    
indeed write down the file , but because the demand need to write a byte array , it is not written into          
  
You do not upload it ?   
form enctype = "multipart / form-data" is set up ?   
is a post request it ?   
If yes get request length is -1  
android client pass up , and wait for the next confirmation with them next , just spent a stupid way

File file = new File("F:\\abc.jpg");


FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[1024];
while((len = in.read(bytes))!=-1){
fos.write(bytes, 0, len);
fos.flush();
}
fos.close();
System.out.println(file.length());
in = new FileInputStream(file);
byte[] temp = new byte[(int) file.length()];
in.read(temp);

first written to a temporary file, get the file size , and then succeeded.

------ For reference only ---------------------------------- -----

request.getContentLength () to obtain a value of -1 , the file is indeed pass up , because I test     
    

// FileOutputStream fos = new FileOutputStream("F://123.jpg");
// int len = 0;
// byte[] bytes = new byte[1024];
// while((len = in.read(bytes))!=-1){
// fos.write(bytes, 0, len);
// fos.flush();
// }
// fos.close();
    
indeed write down the file , but because the demand need to write a byte array , it is not written into          
  
You do not upload it ?   
form enctype = "multipart / form-data" is set up ?   
is a post request it ?   
If yes get request length is -1  
Will get under way if it is submitted , then there is no other way to get to the size of the file uploaded onto the

没有评论:

发表评论