Results 1 to 19 of 19
- 12-20-2008, 08:28 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received in different request
Hi All,
Im new to forum and to java . Im devloping an application that uses HttpServlet for receiving request from mobile device.
Im uploading an image from mobile to server and saving it on server.
I am accepting one chunk in one request and saving it locally.
But i dont know how to save second chunk along with first. Means append next to previous.
Please need help. If any body knows please tell me.
If any can give code snippet then it will be great
- 12-23-2008, 12:10 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
I believe by saving locally you mean you are writing the byte stream to any file, right? So what is the problem are you facing appending the second byte stream chunk to file?
- 12-24-2008, 07:19 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes receved in different request
How can i store it?
Open file in ByteArrayOutputStream and appending the bytes received in second chunk will create image properly?
Im not getting how to do that?
- 12-24-2008, 07:24 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
I think so , appending should work, did u try appending the next chunk to the file.
- 12-24-2008, 08:00 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving byte r3eceived in diffferent request
I tried butit wont work.
Here im pasting my code
File file = new File(path);
if (!file.exists()) {
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(path);
fos.write(imarr);
fos.close();
} catch (SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else{
int len = (int)file.length();
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(path));
ByteArrayOutputStream baOutStream = new ByteArrayOutputStream();
int read = -1;
byte[] tmpArray = null;
while(true){
tmpArray = new byte[CHUNK_SIZE];
read = fis.read(tmpArray);
if(read > -1){
baOutStream.write(tmpArray);
}else{
break;
}
tmpArray = null;
}
tmpArray = null;
baOutStream.write(imarr);
tmpArray = baOutStream.toByteArray();
System.out.println(" tmp array size "+tmpArray.length);
baOutStream.close();
file.delete();
System.out.println(" file exists ");
FileOutputStream fos = new FileOutputStream(path);
fos.write(tmpArray);
fos.close();
}
Can you tell me is there anything to change.
Its not working.? Image is not displaying properly
- 12-24-2008, 08:47 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
see if it helps
In my code I am opening a file, reading a chunk of 10 bytes , opening another file in append mode (see the 2nd argument in FileOutputStream as true, its used to open file in append mode, default is false which opens the file in overwrite mode), and appending contents to the file.
import java.io.*;
class CopyFileByChunk{
private static final int CHUNK_SIZE=10;
public static void main(String args[]){
try{
FileInputStream fis=new FileInputStream("c:\\screenShot.jpg");
byte b[]=new byte[CHUNK_SIZE];
int read=-1;
File fl=new File("d:\\dog.jpg");
if(fl.exists()){
fl.delete();
}
//we open the output file in append mode
FileOutputStream fos=new FileOutputStream(fl,true);
while(true){
read=fis.read(b);
if(read>-1)
fos.write(b);
else
break;
}
}
catch(Exception e){}
}
}
- 12-24-2008, 08:57 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
I am having a look on your code as well.
- 12-24-2008, 09:22 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
//considering your code, I think writing this much of code should solve your problem, however I did not have any way to test
File file = new File(path);
//if the file does not exist, a new file is created else the file is opened in append mode
FileOutputStream fos = new FileOutputStream(path,true);
//I believe imarr is the array which contains the chunk data
fos.write(imarr);
fos.close();
/*one more think which you may have to keep in mind is what happens when two users upload their data from two different mobile devices, the data is appended to the same file, so as per me a possible solution could be you generate the file dynamically i.e. different file name for different users/mobile device. I hope it helps. */
- 12-24-2008, 09:52 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received
Thanks.
By using your code i got idea and in last actually you paste code i wrote.
Thanks
- 12-26-2008, 10:07 AM #10
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received in different request
Im generating path dynamically according to userId in the request path is generated.
One more thing when i upload two images for third image server send response as
HTTP_UNAVAILABLE 503: The server is currently unable to handle the request due to a temporary overloading or maintenance of the server.
Is there any way to get out of this?
- 12-26-2008, 10:22 AM #11
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
You mean your first two images are getting uploaded properly, and the third image is creating problem. Just print some exception stack trace at server and see is it raising any kind of exception, when u try to upload the third image.
- 12-26-2008, 10:54 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received in different request
The thing is when i try to upload data usinglocalhost:8080 i am able to upload many images more than 4.
When i uplaod the class files to our server and try to upload images then it wont work.
At server we cant see the what is exception.
What to do?
- 12-26-2008, 11:01 AM #13
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received in different request
When im upladoing images, the total request become 80 for 2 images
But when i uplaod video it was about 200 still at that im not getting 503.
- 12-26-2008, 11:05 AM #14
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
As per your comments nothing seems to be wrong with your coding part, because the same code should not behave differently in different machines. But without having a look at the server log/config files its tough to say what exactly is going wrong. Is it happening all the times. What looks to me is it has got something to do with server configuration. Is it happening all the times, I mean is it happening in each time you try to upload the third image? What happens if you close you midlet app and upload image after re starting?
- 12-26-2008, 11:22 AM #15
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
It might have got something to do with the gateway as well, but honestly speaking its tough for me confirm from here.
- 12-26-2008, 11:28 AM #16
Member
- Join Date
- Dec 2008
- Posts
- 12
- Rep Power
- 0
saving bytes received in different request
Its not happening reguraly.
Now three images are uploaded. After that i have uploaded 5 images immediately after uplaoding three.
But before that i have restarted our tomcat server. May because of that they get uploaded. What when there are so many mobiles and so many requests came to server.
IS there anything that we can do for avoiding situation of overloading?
- 12-26-2008, 11:50 AM #17
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Is the tomcat and jre version in both machines same, as u have said u r not facing any problem in localhost.
- 12-26-2008, 12:05 PM #18
use a session object!!! :)
- 12-26-2008, 12:12 PM #19
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
@renuka_anil
As per my knowledge tomcat is not very suitable for handling too many requests at a time, and thats why its hardly used in real time scenarios. So I will suggest you to have a look on the servers like JBoss, Websphere etc, because these have a much better memory and thread management techniques. Even if its possible in tomcat , honestly its beyond my knowledge because its totally the server configuration/administration part.
Similar Threads
-
How can you get the exact size of a file in bytes.
By J-Live in forum New To JavaReplies: 7Last Post: 10-28-2008, 01:41 PM -
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM -
midi bytes
By willemjav in forum Advanced JavaReplies: 77Last Post: 07-29-2008, 03:10 PM -
Reading bytes from InputStream
By Java Tip in forum Java TipReplies: 0Last Post: 11-25-2007, 07:51 PM -
how to know the number of bytes
By gabriel in forum New To JavaReplies: 2Last Post: 08-06-2007, 05:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks