You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Burgess, Jay" <jb...@digarch.com> on 2001/03/05 22:55:09 UTC

RE: saving a file on client's machine using servlets

I believe one solution is to POST the form data from the text area to you
servlet, then return it right back, but with an unknown MIME type.
Typically, browsers give you the option to "Save to Disk" when they
encounter an unknown MIME type. To set the MIME type on the response, simply
call "res.setContentType(type);", where "type" is something unknown like
"lakshmi/post".

Passing this data back and forth doesn't seem to be the most efficient
solution, though, so I'm interested to see if there are others.

Jay

-----Original Message-----
From: Adilakshmi Lingam [mailto:alingam@cisco.com]
Sent: Monday, March 05, 2001 3:57 PM
To: tomcat-user@jakarta.apache.org
Subject: saving a file on client's machine using servlets


Hi,
I have a small development question in servlets.
If a user clicks on a button in a web page(servlet generated), a dialog box
should appear asking him the location to save the content. and the contents
of a text area in the web page have to be store in a file on the client's
machine.
Any ideas please,
Thanks,
Lakshmi









---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-user-help@jakarta.apache.org

RE: saving a file on client's machine using servlets

Posted by Scott Walter <tx...@yahoo.com>.
Below is some code I have written which will take a
file that was uploaded from an html page that has the
file control on it to a servlet.  The servlet then
inserts the file into a database, it can be easily
modified to save to a disk file.  FYI--I use the
o'reilley package for working with fileparts within
the http header so you will need to download it from
o'reilley to get it to work.

package sitemanager.shr.upload;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.multipart.*;
import sitemanager.shr.app.AppManager;
import java.sql.*;

/**
 *  Description of the Class
 *
 *@author     Scott F. Walter
 *@created    February 17, 2001
 */
public class UploadContent extends HttpServlet {

	/**
	 *  Description of the Method
	 *
	 *@param  request               Description of
Parameter
	 *@param  response              Description of
Parameter
	 *@exception  IOException       Description of
Exception
	 *@exception  ServletException  Description of
Exception
	 */
	public void doGet(HttpServletRequest request,
			HttpServletResponse response)
			 throws IOException, ServletException {
	}


	/**
	 *  Description of the Method
	 *
	 *@param  request               Description of
Parameter
	 *@param  response              Description of
Parameter
	 *@exception  IOException       Description of
Exception
	 *@exception  ServletException  Description of
Exception
	 */
	public void doPost(HttpServletRequest request,
			HttpServletResponse response)
			 throws IOException, ServletException {
		MultipartParser mp = new MultipartParser(request,
1024 * 1024);
		Part p = mp.readNextPart();

		Connection conn = null;
		byte[] fileData = null;
		byte[] fileData2 = null;

		try {
			conn = AppManager.getConnection();

			//while(p != null) {
			if (p instanceof FilePart) {
				ByteArrayOutputStream ba = new
ByteArrayOutputStream();
				((FilePart) p).writeTo(ba);
				fileData = ba.toByteArray();

				PreparedStatement stmt =
conn.prepareStatement("INSERT INTO sm_banners
(filename,bannerimage) values(?,?)");
				stmt.setString(1, ((FilePart) p).getFileName());
				stmt.setBytes(2, fileData);
				stmt.executeUpdate();
				stmt.close();

				Statement stmt2 = conn.createStatement();
				ResultSet rs = stmt2.executeQuery("select
bannerimage from sm_banners");
				rs.next();
				fileData2 = rs.getBytes(1);

			}
			//p = mp.readNextPart();
			//}
		}
		catch (Exception e) {
			sitemanager.util.Logger.log(e);
		}
		finally {
			try {
				conn.close();
			}
			catch (Exception e) {
				sitemanager.util.Logger.log(e);
			}
		}

		response.setContentType("text/html");
		ServletOutputStream out =
response.getOutputStream();
		out.println("the file was uploaded!");
		out.close();
	}
}


--- Adilakshmi Lingam <al...@cisco.com> wrote:
> Can you please point me to some info on how to do
> that. I'm kind of a newbie
> in this area.
> thanks in advance,
> Lakshmi
>   -----Original Message-----
>   From: dave@dcc.vu [mailto:dave@dcc.vu]On Behalf Of
> David Crooke
>   Sent: Monday, March 05, 2001 11:28 PM
>   To: tomcat-user@jakarta.apache.org
>   Subject: Re: saving a file on client's machine
> using servlets
> 
> 
>   Sounds like a case for JavaScript, or possibly an
> applet. Pushing to the
> server and back is kinda clonky.
>   If you just want them to create a file and save to
> disk, why not have them
> crank up a local text editor?
> 
>   "Burgess, Jay" wrote:
> 
> 
>     I believe one solution is to POST the form data
> from the text area to
> you servlet, then return it right back, but with an
> unknown MIME type.
> Typically, browsers give you the option to "Save to
> Disk" when they
> encounter an unknown MIME type. To set the MIME type
> on the response, simply
> call "res.setContentType(type);", where "type" is
> something unknown like
> "lakshmi/post".
> 
>     Passing this data back and forth doesn't seem to
> be the most efficient
> solution, though, so I'm interested to see if there
> are others.
> 
>     Jay
> 
>     -----Original Message-----
>     From: Adilakshmi Lingam
> [mailto:alingam@cisco.com]
>     Sent: Monday, March 05, 2001 3:57 PM
>     To: tomcat-user@jakarta.apache.org
>     Subject: saving a file on client's machine using
> servlets
> 
>     Hi,
>     I have a small development question in servlets.
>     If a user clicks on a button in a web
> page(servlet generated), a dialog
> box
>     should appear asking him the location to save
> the content. and the
> contents
>     of a text area in the web page have to be store
> in a file on the
> client's
>     machine.
>     Any ideas please,
>     Thanks,
>     Lakshmi
> 
> 
> 
> 
> 
> 
> 
> 
>    
>
---------------------------------------------------------------------
>     To unsubscribe, e-mail:
> tomcat-user-unsubscribe@jakarta.apache.org
>     For additional commands, email:
> tomcat-user-help@jakarta.apache.org
> 
> 


=====
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scott
May the Force be with you!

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

RE: saving a file on client's machine using servlets

Posted by Adilakshmi Lingam <al...@cisco.com>.
Can you please point me to some info on how to do that. I'm kind of a newbie
in this area.
thanks in advance,
Lakshmi
  -----Original Message-----
  From: dave@dcc.vu [mailto:dave@dcc.vu]On Behalf Of David Crooke
  Sent: Monday, March 05, 2001 11:28 PM
  To: tomcat-user@jakarta.apache.org
  Subject: Re: saving a file on client's machine using servlets


  Sounds like a case for JavaScript, or possibly an applet. Pushing to the
server and back is kinda clonky.
  If you just want them to create a file and save to disk, why not have them
crank up a local text editor?

  "Burgess, Jay" wrote:


    I believe one solution is to POST the form data from the text area to
you servlet, then return it right back, but with an unknown MIME type.
Typically, browsers give you the option to "Save to Disk" when they
encounter an unknown MIME type. To set the MIME type on the response, simply
call "res.setContentType(type);", where "type" is something unknown like
"lakshmi/post".

    Passing this data back and forth doesn't seem to be the most efficient
solution, though, so I'm interested to see if there are others.

    Jay

    -----Original Message-----
    From: Adilakshmi Lingam [mailto:alingam@cisco.com]
    Sent: Monday, March 05, 2001 3:57 PM
    To: tomcat-user@jakarta.apache.org
    Subject: saving a file on client's machine using servlets

    Hi,
    I have a small development question in servlets.
    If a user clicks on a button in a web page(servlet generated), a dialog
box
    should appear asking him the location to save the content. and the
contents
    of a text area in the web page have to be store in a file on the
client's
    machine.
    Any ideas please,
    Thanks,
    Lakshmi








    ---------------------------------------------------------------------
    To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
    For additional commands, email: tomcat-user-help@jakarta.apache.org


Re: saving a file on client's machine using servlets

Posted by David Crooke <da...@convio.com>.
Sounds like a case for JavaScript, or possibly an applet. Pushing to the
server and back is kinda clonky.

If you just want them to create a file and save to disk, why not have
them crank up a local text editor?

"Burgess, Jay" wrote:

>
>
> I believe one solution is to POST the form data from the text area to
> you servlet, then return it right back, but with an unknown MIME
> type.  Typically, browsers give you the option to "Save to Disk" when
> they encounter an unknown MIME type. To set the MIME type on the
> response, simply call "res.setContentType(type);", where "type" is
> something unknown like "lakshmi/post".
>
> Passing this data back and forth doesn't seem to be the most efficient
> solution, though, so I'm interested to see if there are others.
>
> Jay
>
> -----Original Message-----
> From: Adilakshmi Lingam [mailto:alingam@cisco.com]
> Sent: Monday, March 05, 2001 3:57 PM
> To: tomcat-user@jakarta.apache.org
> Subject: saving a file on client's machine using servlets
>
> Hi,
> I have a small development question in servlets.
> If a user clicks on a button in a web page(servlet generated), a
> dialog box
> should appear asking him the location to save the content. and the
> contents
> of a text area in the web page have to be store in a file on the
> client's
> machine.
> Any ideas please,
> Thanks,
> Lakshmi
>
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org

RE: saving a file on client's machine using servlets

Posted by Adilakshmi Lingam <al...@cisco.com>.
RE: saving a file on client's machine using servletsThanks, Jay.
That worked perfectly.
  -----Original Message-----
  From: Burgess, Jay [mailto:jburgess@digarch.com]
  Sent: Monday, March 05, 2001 3:55 PM
  To: 'tomcat-user@jakarta.apache.org'
  Subject: RE: saving a file on client's machine using servlets


  I believe one solution is to POST the form data from the text area to you
servlet, then return it right back, but with an unknown MIME type.
Typically, browsers give you the option to "Save to Disk" when they
encounter an unknown MIME type. To set the MIME type on the response, simply
call "res.setContentType(type);", where "type" is something unknown like
"lakshmi/post".

  Passing this data back and forth doesn't seem to be the most efficient
solution, though, so I'm interested to see if there are others.

  Jay

  -----Original Message-----
  From: Adilakshmi Lingam [mailto:alingam@cisco.com]
  Sent: Monday, March 05, 2001 3:57 PM
  To: tomcat-user@jakarta.apache.org
  Subject: saving a file on client's machine using servlets



  Hi,
  I have a small development question in servlets.
  If a user clicks on a button in a web page(servlet generated), a dialog
box
  should appear asking him the location to save the content. and the
contents
  of a text area in the web page have to be store in a file on the client's
  machine.
  Any ideas please,
  Thanks,
  Lakshmi










  ---------------------------------------------------------------------
  To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
  For additional commands, email: tomcat-user-help@jakarta.apache.org