Hi!
For me this works great:
----client-----
HttpURLConnection uc = (HttpURLConnection)new URL("...").openConnection();
File mp3File = ...;
uc.setRequestProperty("fName", mp3File.getName());
uc.setRequestMethod("POST");
BufferedOutputStream bout = new
BufferedOutputStream(uc.getOutputStream(),256);
BufferedInputStream bin = new BufferedInputStream(new
FileInputStream(mp3File),256);
byte[] b=new byte[256];
int n=-1;
while ((n = bin.read(b,0,b.length))!=-1)
bout.write(b,0,n);
bin.close();
bout.close();
------client---------
-----server-------
String fName = request.getHeader("fName");
BufferedInputStream bin = new BufferedInputStream(request.getInputStream());
File mp3File = new File(getServletContext().getRealPath("/mp3/"),fName);
if (!mp3File.exists()) mp3File.createNewFile();
FileOutputStream fout = new FileOutputStream(
byte[] b=new byte[256];
int n=-1;
while ((n=bin.read(b,0,b.length))!=-1)
fout.write(b,0,n);
bin.close();
fout.close();
------server-----------
Hope this helps!
-mw
----- Original Message -----
From: "Peter Maas" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, November 02, 2002 1:00 PM
Subject: Re: sending a file from Java application to servlet
> Well... ok... works fine... but still the binary transfer doesn't seem to
> work.. I'm currently using a CORBA workarround...
>
> P
>
> ----- Original Message -----
> From: "Michael Weller" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, November 02, 2002 12:37
> Subject: Re: sending a file from Java application to servlet
>
>
> > Hi!
> > Why don't you simply use
URLConnection.setRequestProperty("fName","value")
> > to pass the mp3's filename to the servlet? On the server-side you'd
simply
> > do a request.getHeader("fName") to get the name.
> >
> > -mw
> >
> > ----- Original Message -----
> > From: "Peter Maas" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, November 01, 2002 6:33 PM
> > Subject: Re: sending a file from Java application to servlet
> >
> >
> > > I tried doing that and it almost works... but... (probably a realy
> stupid
> > > mistake)....
> > > I would like to transfer an mp3 file to the servlet this is how the
code
> > in
> > > the app looks:
> > >
> > > DataOutputStream printout = new
> > > DataOutputStream(urlC.getOutputStream());
> > >
> > > File theFile = new File(file);
> > > String fname = "filename="+theFile.getName();
> > > printout.writeBytes(fname);
> > > printout.writeBytes("\n");
> > >
> > > FileInputStream fos;
> > > DataInputStream ds;
> > > try {
> > > fos = new FileInputStream(file);
> > > ds = new DataInputStream( fos );
> > > byte[] data = new byte[(int)theFile.length()];
> > > ds.readFully(data);
> > > printout.write(data);
> > > ds.close();
> > > fos.close();
> > > }
> > > catch (IOException ioe) {
> > > System.out.println( "IO error: " + ioe );
> > > }
> > >
> > > printout.flush();
> > > printout.close();
> > >
> > > The servlet side's code looks like this:
> > >
> > > /**
> > > * Below we extract the filename from the
> > > * Http headers read so far.
> > > */
> > >
> > > fFilename =
> > >
> >
>
pLine.substring(9+pLine.lastIndexOf("filename="),pLine.lastIndexOf("mp3")+3)
> > > ;
> > > System.out.println(fFilename);
> > >
> > > /*
> > > * Write the remaining to disk
> > > */
> > >
> > > FileOutputStream fos;
> > > DataOutputStream ds;
> > > try {
> > > fos = new FileOutputStream(UploadLocation+fFilename);
> > > ds = new DataOutputStream( fos );
> > > String line = br.readLine();
> > > if(line != null){
> > > ds.writeBytes(line);
> > > while((line = br.readLine()) != null){
> > > // if there is a newline in the header it probably came from the
> > binary
> > > data
> > > ds.writeBytes("\n");
> > > ds.writeBytes(line);
> > > }
> > > }
> > > ds.close();
> > > fos.close();
> > > }
> > > catch (IOException ioe) {
> > > System.err.println( "IO error: " + ioe );
> > > }
> > >
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html