Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file upload..urgent..

 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
can anyone please tell me how to upload a file from hard disc to a server in java.a piece of sample code would be of great help.
regards
karan
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
i found the following piece of code but it gives me errors in the first two import statements and the getOutputStream statemets,it says "cannot resolve symbol",does it mean that i got to have some netscape class files in order to run it?please help me out..its urgent..i would really appreciate it.
import netscape.server.applet.HttpApplet;
import netscape.server.applet.ServerApplet;
import java.io.*;
import java.lang.*;
import java.net.Socket;
import java.net.InetAddress;
import java.util.Hashtable;
import java.util.Enumeration;
class UploadApplet extends HttpApplet {
public void run() throws Exception {
int cclen, bytesread = 0;
int totbytes = 0;
int bytesremain = 0;
int bytesavail = 0;
String line;
byte b[] = new byte[9192];
line = getHeader("content-length");
cclen = Integer.parseInt(line);
PrintStream out = getOutputStream();
InputStream is = getInputStream();
DataInputStream di = new DataInputStream(is);
if (returnNormalResponse("text/plain")) {
out.print(header);
out.println(cclen);
out.flush();
FileOutputStream fos = new FileOutputStream("/tmp/testfile");
while (totbytes < cclen)
{
bytesread = di.read(b, 0, 8192);
//out.println(bytesread);
//out.flush();
if (bytesread == -1)
break;
fos.write(b, 0, bytesread);
totbytes += bytesread;
}
fos.close();
out.print(trailer);
out.flush();
}
}
private static final String header = "<h1>Write in Progress</h1>\n<ul>\n";
private static final String trailer = "Finished</ul>\n";
}
regards
karan
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karan,
how r u
of course you need some classes. I don't know them exactly but I have used netscape.security classes. I think these are similar. I needed to download those extra classes from the sun site, so I advise you to search there unless some one suggest you more helpful idea.
Regards,
MAC.
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jimi,
hows it going..thanks for the advice..i will search for the class files now,but just out of curiosity,do u know of any better way to upload files in java?i would appreciate any help in this regard.
regards
karan
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi karan
there is an article at www.onjava.com by Budi Kurniwan which gives details about uploading files
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
somebody please tell me if file uploading is possible with java script,if yes then how can i integrate java script with java?
regards
karan
PS-i know its possible with CGI but i dunno how to integrate CGI with java
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karan, I have another suggestion as I have gone through the file uploading process step by step successfully.
I have used a signed applet on the client end and a servlet on the server side.
I can provide you with all necessary steps. I have tried to mail you but your email is unavailable.
I think that keep mailing is good in this case. just email me or provide your email in your javaranch profile if my suggestion is appropriate with your case.
Regards,
MAC.
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jimi,
i have mailed you on the email acct given in your javaranch profile.please read it and reply me back..
regards
karan
 
Anuj Anand
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Karan...
I have a problem similar to yours .need to upload PDF file from client ot server .....Did your code for uploading work..If it has please forward me your code on anuj.anand@cna.com
Your response would be appreciated.
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not only thinking about upload from applet to servlet but also from html (javascript) to servlet, the easiest way will be using com.oreilly.servlet.
Hope it helps
Detlev
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the URL detlev,actually i stumbled across that site before,and i downloaded the zip file from there,but it sounded too complex to make any sense to me,if you could make any sense out of it then plz tell me too..i would really appreciate that.
regards
karan
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this:

The second line handles the upload! Could it be easier? I do not think so. Browse through the API-docs; they are ok.
Hope it helps
Detlev
 
Kavitha, DP
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is a package available for uploading files and images etc for free at http://www.jspsmart.com/ It is ofcourse to upload files from html using JSP. Hope this serves your purpose.
 
Raghvendra Sharma
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
can anyone put some light on the methods available for uploading a file from Applet without signing it... (I don't think there are, but i have found a work around for IE which doesn't work in Netscape 6.x )
If there is any such method, it'll be of great help to me..
Links would also do better.
regards
raghav..
 
Jimi Rock
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Raghav, Applet cannot access local system resources without being signed. Unfortuantly,Signing mechanisms differ from one browser to another.
I have gone throgh signing applet with a lot of pain because lack of documentation over the web. at last, I have found that the best way is to sign for java plugin so that ur applet can run independent of the client browser.
Anuj, the file uploading application that we have and Karan is trying to use is able to upload a file of any type. I hope that ur problem is solved, just tell if any problem occured, we all will try hard to help...
Regards,
MAC.
 
Jonathan W. Brett
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created an applet that allows users multiple file uploads. It is very simple and easy to understand and can change the layout and destinations through parameters in the HTML page.
I have it signed by a certificate from Thawte and this multi-purpose cert. can be used for Netscape and IE. The .cab and .jar file just have to include the appropriate security classes form each browser.
Need anwsers or help, email me at jbrett@roadrunner.nf.net
------------------
Jonathan Brett, SCJP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic