Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Image and .zip file(Uploading)

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam facing with uploading a file.I can upload .txt,.html,.xml.xsl etc.But problem with Image and .zip file it uploaded properly(i mean size of the file is ok) but couldn't open giving error format damaged/parsing error.Iam sending data through http header like that:-
urlConn.setRequestProperty("Content-Type","multipart/form-data; boundary=-----------------------------7d01ecf406a6");

printout = new DataOutputStream (urlConn.getOutputStream ());
String content="-----------------------------7d01ecf406a6\r\nContent-Disposition: form-data; name=\"upload\"; filename=\""+aa+"\"\r\nContent-Type: application/octet-stream\r\n\r\n" + conffile + "\r\n-----------------------------7d01ecf406a6--\r\n";
printout.writeBytes(content);
printout.flush ();
printout.close ();
Plz guide me where I done a mistake.
Best Regards
Bikash
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I also tried with Content-Type:image/gif or image/jpg .But couldn't slove this problem.
Regards
Bikash
------------------
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean you're able to upload the file (I mean the servlet doesn't throw any errors), but that the file is corrupt when it gets to the other end?

If the servlet throws an error, what is the exact error?

Glad you've got it partly working.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
Thanks for ur reply.Iam facing problem with Image file and .zip file.Those files r uploaded properly(means only size of the files r ok) without throws any error by servlet but on the other end files r not open(giving error files r not valid).But with.txt,.html,.xml,.xsl etc.files r uploaded properly and also opening properly.I couldn't understand where Iam going wrong.Plz guide me.Excepting quick reply.
Regards
Bikash

------------------
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yoiks, you are converting the stream to a String before sending, which could be why all your text types work but binary types aren't.
Have you tried:

Note that this treats conffile as a byte[] and not a String...
(and beware that streams was never a strength of mine )
Dave
[ UBB is out to get me ]
[This message has been edited by David O'Meara (edited December 06, 2001).]
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Thanks for ur guide.I tried with ur given code but it is giving same problem as early.I am sending data from swing interface by selecting file from my local drive.Iam using oreilley's MultipartRequest class and using weblogic 6.0 server.In weblogic I saw the Content-Type.properties class there all type of image file's extension r there.Below r my codes:-
ImageIcon Upload=new ImageIcon("images/Upload.gif");
Button=new JButton(Upload);
Button.setToolTipText("Upload");

Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int returnVal = fc.showOpenDialog(ActionDemo4.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String aa=file.getAbsolutePath();
textArea3.append(aa);
textArea2.append("Local URL:");
long l=file.length();
try
{
byte buff[]=new byte[(int)file.length()];
InputStream fileIn=new FileInputStream(aa);
int i=fileIn.read(buff);
String conffile=new String(buff);

String str1=textArea10.getText();


url = new URL ("http://127.0.0.1:7001/servletUpload?x="+str1);


urlConn = url.openConnection();

urlConn.setDoInput (true);

urlConn.setDoOutput (true);

urlConn.setUseCaches (false);
If u have any solution about it plz extend to me.
Best Regards
Bikash
------------------
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have doubts that URLConnection is the correct way to go. I don't think that it builds the HTTP header correctly for a multipart/form-encoded request.
Try this program I use (I think I wrote it all but it's been a while) It behaves like a fake server except that it accepts everything, gathers the HTTP header, turns it into a web page and sends it back.

Get your program to submit to it (http://localhost/) and see what it returns, then comare it to the format from this:

(don't worry, we'll get it working!)
Dave.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
I couldn't understand how I try with ur given programme.I tried like that.I put ur programme in my servlet directory of my weblogic server.And In my swing interface file I changed this line[url = new URL ("http://127.0.0.1:7001/BrowserTest");]which is in bin folder of jdk1.2.2.Actually I couldn't run ur programme.But ur given programme complie perfectly.Can u plz tell me elaborately how I will run ur programme.Iam waiting for ur reply.
Regards
Bikash
------------------
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Now I am able to run ur programme. I run it like that in one Dos-prompt I run ur programme. And in another Dos-prompt I run my swing Interface programme.when I select Image file through my swing Interface from local hard drive then it is giving error in Dos-prompt in which ur programme is running like that:-
[C:\jdk1.2.2\bin>java BrowserTest
LISTENING...
Got incomming request
Exception in run() : Unable to read request in readInput()
Got incomming request]
And in response textArea of swing interface showing like that
--------------------------------------------------------------
[Your Selected File Name is C:\jdk1.2.2\bin\images\Q_connect.gif
File Size Is 227bytes
Please Wait ! Upload Is in Progress<h2>Request Header:</h2>
<hr>
<font face='monospace'>
POST /BrowserTest?x= HTTP/1.0<br>
Content-Type: multipart/form-data; boundary=-----------------------------7d11e410e500f2<br>
User-Agent: Java1.2.2<br>
Host: 127.0.0.1<br>
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2<br>
Content-length: 453<br>
-----------------------------7d01ecf406a6<br>
Content-Disposition: form-data;name="upload"; filename="C:\jdk1.2.2\bin\images\Q_connect.gif"<br>
Content-Type:application/octet-stream<br>
GIF89a2]
----------------------------------------------------------
But when I select .txt,.html etc. file it is not giving error in Dos-prompt.Then it is showing only[Got incomming request].
Now from this I reached at one conclusion with Image file my servlet can't read request in readInput.For this what should I do.Plz guide me.
Plz tell way of running ur given programme is correct or not.
Regards
Bikash
------------------
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Plz help me.
Regards
Bikash
------------------
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before trying to use David's code (in his second post), did you try the suggestion he made in his first post?

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
I already tried with David first given suggestion but It not slove my problem.Now I sloved my problem and image file now working perfectly just like that:-
printout.write(buff);instead of printout.writBytes(conffile);
Thanks
Bikash
------------------
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Now Iam facing problem with .zip file it uploaded properly but not unzip.If u have any solution plz extend to me.
Thanks for ur valuable time
Bikash
------------------
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!