You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Florian Kiebel <fl...@matamore.com> on 2004/05/31 19:01:31 UTC

[FileUpload] Problems with chaining uploading

Hello!

I have some problems with FileUpload. So I have two jsp scripts.
First one is the form script testUpload.jsp... There are an hidden field, a
form field and a file field. Here is the code: (my apologizes for the
comment in French)

<%@ page language="java" %>
<%@ page import="modula.*" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">

<form name="upload" method="post" action="upload.jsp"
enctype="multipart/form-data" >
<input type="hidden" name="nbPiecesJointes" value="<%=
request.getParameter("nb") %>">
<div style="text-align:center; width : 99%;">
	<!-- Titre de la page -->
	<div class="titre">.: CREER UN MARCHE - ETAPE 3b :.</div>
	<!-- /Titre de la page -->
	<br />
	<!-- Rappel infos administratives -->
	<!-- /Rappel infos administratives -->
	<br />
	<!-- Gestion des pièces jointes obligatoires -->
	<div class="result">
		<div>
			<table width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="titre_gris">.: Piece Jointe :.</td>
					<td class="titre_gris" style="text-align:right;">
					<strong>Reste : <%= request.getParameter("nb") %></strong></td>
				</tr>
			</table>
		</div>
		<div class="contenu">
			 <table width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td style="width : 50% ; text-align : right;">
<%
	MarchePieceJointeType[] listeTypes =
MarchePieceJointeType.getAllMarchePieceJointeType();
%>
						<select name="typeFile">
							<option> Choisissez un type de pièce jointe </option>
<%
		for (int j = 0; j < listeTypes.length; j++)
		{
%>
							<option value="<%= listeTypes[j].getIdType() %>">
							<%= listeTypes[j].getTypeDocument() %></option>
<%
		}
%>
						</select>
					</td>
					<td style="text-align : left;">
					<input type="file" name="file">
					</td>
				</tr>
			</table>
		</div>
	</div>
	<!-- /Gestion des pièces jointes obligatoires -->
	<br />
</div>
<div align="center"><input type='submit' name='submit' value="Valider les
informations de l'étape 3" >
&nbsp;<input type='reset' name='RAZ' value='Annuler' ></div>
</form>

</body>
</html>

And I have the second script named upload.jsp which must parse the request
and upload file.
There is also in this script another form which permits to redirect the page
towards the testUpload.jsp with the hidden field nbPiecesJointes minus 1.
Here is the code of upload.jsp:
<%@ page import="modula.*" %>
<%@ page import="java.io.*, java.util.*, org.w3c.dom.*,
org.apache.commons.fileupload.*" %>
<%
System.out.println("******* upload.jsp *******");
// PieceJointeProposition pjprop = new PieceJointeProposition
(beanUser.cnDBConn);
String fileNameInServer;
int  yourMaxMemorySize = 1024 * 1024 * 8;
int  yourMaxRequestSize = 1024 * 1024 * 8;
int nbPiecesJointes = -1;
String yourTempDirectory = "c:\\temp\\";

fileNameInServer="";

DiskFileUpload upload = new DiskFileUpload();


// Set upload parameters
upload.setSizeThreshold(yourMaxMemorySize);
upload.setSizeMax(yourMaxRequestSize);
upload.setRepositoryPath(yourTempDirectory);

// Parse the request
System.out.println("Parse the request");


// Parse the request
List items = null;
try {
	items = upload.parseRequest(request, yourMaxMemorySize, yourMaxRequestSize,
yourTempDirectory);
} catch (FileUploadException e) {
	//e.printStackTrace();
	//throw new IOException("could not be uploaded");
	out.write("le fichier ne peut pas être chargé : fichier trop
volumineux<BR>");
	out.write("taille maximale autorisée : " + yourMaxRequestSize / (1024 *
1024) + "Mo");
	return;
}

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
    FileItem item = (FileItem) iter.next();

    System.out.println("get item");

    if (item.isFormField()) {
    	out.write("<br />****** form field ********<br />");
    	//out.write(item.getContentType());
    	out.write(item.getFieldName());
    	out.write(item.getString());
    	if (item.getFieldName().equals("nbPiecesJointes"))
    	{
    		nbPiecesJointes = Integer.parseInt(item.getString());
    		nbPiecesJointes--;
    		out.write("Modification nbPiecesJointes = " + nbPiecesJointes);
    	}
    	if (item.getFieldName().equals("typeFile"))
    	{
    		int typeFile = -1;
    		if (item.getString().equals(""))
    		{
    			out.write("ERREUR : aucun type choisi");
    		}
    		else
    		{
    			typeFile = Integer.parseInt(item.getString());
    			out.write("champ typeFile = " + typeFile);
    		}
    	}
    	out.write("<br />****** /form field ********<br />");
    } else {
    	out.write("<br />****** file field ********<br />");
	    String fieldName = item.getFieldName();
	    String pathfileName = item.getName();
	    String fileName ;
	    String sPrompt ;
	    String contentType = item.getContentType();
	    boolean isInMemory = item.isInMemory();
	    long sizeInBytes = item.getSize();

	    String[] parts = pathfileName.split("[\\\\/]");
	    fileName = parts[parts.length-1];

	    sPrompt = "fieldName=" + fieldName + "<BR>"
		 + "fileName=" + fileName + "<BR>"
	    	 + "pathfileName=" + pathfileName + "<BR>"
	     	 + "contentType =" + contentType + "<BR>"
	    	 + "isInMemory =" + isInMemory + "<BR>"
	         + "sizeInBytes =" + sizeInBytes + "<BR>";

	    System.out.println(sPrompt);
	    out.write(sPrompt);

	    InputStream uploadedStream ;

	    String pathFileNameCrypted = "";
	    File uploadedFile = new File(yourTempDirectory + fileName);
	    //item.write(uploadedFile);

	    // écrit le fichier dans le repertoire temporaire
	    //uploadedStream = item.getInputStream();
	    item.write(uploadedFile );
	    item.delete();
		out.write("Fichier uploadé");
		/*
		FileWriter fw =  new FileWriter (uploadedFile );
	    fw.write(uploadedStream);
		fw.flush();
		fw.close();
		*/

		// chiffrer le fichier
		//pathFileNameCrypted = GnuPG.encrypt(fileName ,yourTempDirectory ,
"F5A1C6A7");

		//sPrompt = "fileName =" + fileName + "<BR>"
		//	+ "pathFileNameCrypted =" + pathFileNameCrypted + "<BR>";

		//System.out.println(sPrompt);
		//out.write(sPrompt);

		// monter le fichier dans la base
		out.write("<br />****** /file field ********<br />");
    }
}
%>
<form name="upload" method="post" action="testUpload.jsp?nb=<%=
nbPiecesJointes %>" >
<input type="submit" value="Continuer" />
</form>
<%
System.out.println("****** /upload.jsp *******");
//response.sendRedirect("vide.html");
%>

So I am chaining the different scripts but I have a problem with the chain
since there is an exception when I attempt to upload big files ( but size <
8 Mo)
For exemple I upload a file of 6787600 bytes with no problems but when I
want upload a file for the second loop I have an exception error with the
context. I don't understand the problem. I suspect a problem of buffering
but I don't know what is it...

If someone could help me to fix my loop, I would be very interested...

Thanks to all

Florian Kiebel
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004



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


RE: [FileUpload] Problems with chaining uploading

Posted by Tatu Vanhanen <ta...@graftor.fi>.
> From: Schalk [mailto:schalk@volume4.co.za]
...
> You can go to http://www.volume4.co.za/phpBB2/viewtopic.php?t=2

This one also potentially tries to redirect a post request, which is not
allowed (some browsers may still do it) as Martin Cooper said previously.
I'd prefer forwarding the request processing to the "product_added.jsp", or
whatever jsp with
request.getRequestDispatcher("/product_added.jsp").forward(request,
response);

in the upload servlet. This forwards the processing in the container to the
next component (product_added.jsp) that will present the view (i.e. show you
the results of uploading). You can store some status info as request
attributes in the upload servlet and use (show) them in the jsp view.

> :: I am sorry to use jsp pages but perhaps (surely) it's a silly question,
> the
> :: jsp pages aren't compiled into servlet? Perhaps I didn't correctly
> :: understand the principles of jsp and servlets...
> ::

It is considered a better practice (compared to just jsp pages) to do
_processing_ in servlets and _presenting_ in jsp pages and alike (Velocity
templates?). Look at some other people's jsp pages containing mixed java and
html code in them and try to resolve what the page does; then you know why
;)


But back to the Exception you have, a stack trace will probably tell
something about the reason.

- Tatu V.


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


RE: [FileUpload] Problems with chaining uploading

Posted by Schalk <sc...@volume4.co.za>.
You can go to http://www.volume4.co.za/phpBB2/viewtopic.php?t=2

Kind Regards
Schalk Neethling
Web Developer.Designer.Programmer.President
Volume4.Development.Multimedia.Branding
emotionalize.conceptualize.visualize.realize
Tel: +27125468436
Fax: +27125468436
email:schalk@volume4.co.za
web: www.volume4.com
 
This message contains information that is considered to be sensitive or
confidential and may not be forwarded or disclosed to any other party
without the permission of the sender. If you received this message in error,
please notify me immediately so that I can correct and delete the original
email. Thank you. 

:: -----Original Message-----
:: From: Florian Kiebel [mailto:florian.kiebel@matamore.com]
:: Sent: Tuesday, June 01, 2004 11:50 AM
:: To: 'Jakarta Commons Users List'
:: Subject: RE: [FileUpload] Problems with chaining uploading
:: 
:: Hello Martin,
:: 
:: I am sorry to use jsp pages but perhaps (surely) it's a silly question,
the
:: jsp pages aren't compiled into servlet? Perhaps I didn't correctly
:: understand the principles of jsp and servlets...
:: 
:: But why does it work correctly with one file and not the other?
:: Actually, I think Commons-Fileupload lacks of documentation... There is
no
:: example of use of this package or I didn't found them...
:: 
:: Do you know any links which permits me to solve my problem of servlet?
:: (Links with complete examples like servlet code and/or form code)
:: 
:: And other question, I didn't understand your last reported problem. Does
it
:: mean I must use a "response.sendRedirect()" instead of a
:: "request.sendRedirect()" ?
:: 
:: I hope you could help me, as you surely guess I am a newbie in the jsp
and
:: servlet world so I try to learn a maximum of things.
:: 
:: Nevertheless, I thank you for your time...
:: 
:: Florian Kiebel
:: 
:: -----Message d'origine-----
:: De : Martin Cooper [mailto:martinc@apache.org]
:: Envoyé : lundi 31 mai 2004 19:28
:: À : Jakarta Commons Users List
:: Objet : Re: [FileUpload] Problems with chaining uploading
:: 
:: Why, oh why, do so many people insist on using JSP pages for binary
:: request handling that should be in a servlet...
:: 
:: Anyway, I see two related problems (apart from the fact that the code
:: should be in a servlet).
:: 
:: 1) Redirecting POST requests is generally disallowed, and is specifically
:: disallowed by the HTTP spec without explicit confirmation from the end
:: user.
:: 
:: 2) You are writing to the response, and then redirecting the request. You
:: can't do that - a redirect tells the browser to make a separate request
:: for the subsequent page, and having anything committed in the response
:: prior to doing a redirect is pretty much guaranteed to cause problems.
:: 
:: --
:: Martin Cooper
:: 
:: 
:: On Mon, 31 May 2004, Florian Kiebel wrote:
:: 
:: > Hello!
:: >
:: > I have some problems with FileUpload. So I have two jsp scripts.
:: > First one is the form script testUpload.jsp... There are an hidden
field,
:: a
:: > form field and a file field. Here is the code: (my apologizes for the
:: > comment in French)
:: >
:: > <%@ page language="java" %>
:: > <%@ page import="modula.*" %>
:: > <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
:: > <html>
:: > <head>
:: > <title>Lomboz JSP</title>
:: > </head>
:: > <body bgcolor="#FFFFFF">
:: >
:: > <form name="upload" method="post" action="upload.jsp"
:: > enctype="multipart/form-data" >
:: > <input type="hidden" name="nbPiecesJointes" value="<%=
:: > request.getParameter("nb") %>">
:: > <div style="text-align:center; width : 99%;">
:: > 	<!-- Titre de la page -->
:: > 	<div class="titre">.: CREER UN MARCHE - ETAPE 3b :.</div>
:: > 	<!-- /Titre de la page -->
:: > 	<br />
:: > 	<!-- Rappel infos administratives -->
:: > 	<!-- /Rappel infos administratives -->
:: > 	<br />
:: > 	<!-- Gestion des pièces jointes obligatoires -->
:: > 	<div class="result">
:: > 		<div>
:: > 			<table width="100%" cellpadding="0" cellspacing="0">
:: > 				<tr>
:: > 					<td class="titre_gris">.: Piece
:: Jointe :.</td>
:: > 					<td class="titre_gris"
:: style="text-align:right;">
:: > 					<strong>Reste : <%=
:: request.getParameter("nb") %></strong></td>
:: > 				</tr>
:: > 			</table>
:: > 		</div>
:: > 		<div class="contenu">
:: > 			 <table width="100%" cellpadding="0"
:: cellspacing="0">
:: > 				<tr>
:: > 					<td style="width : 50% ; text-align
:: : right;">
:: > <%
:: > 	MarchePieceJointeType[] listeTypes =
:: > MarchePieceJointeType.getAllMarchePieceJointeType();
:: > %>
:: > 						<select name="typeFile">
:: > 							<option> Choisissez
:: un type de pièce jointe </option>
:: > <%
:: > 		for (int j = 0; j < listeTypes.length; j++)
:: > 		{
:: > %>
:: > 							<option value="<%=
:: listeTypes[j].getIdType() %>">
:: > 							<%=
:: listeTypes[j].getTypeDocument() %></option>
:: > <%
:: > 		}
:: > %>
:: > 						</select>
:: > 					</td>
:: > 					<td style="text-align : left;">
:: > 					<input type="file" name="file">
:: > 					</td>
:: > 				</tr>
:: > 			</table>
:: > 		</div>
:: > 	</div>
:: > 	<!-- /Gestion des pièces jointes obligatoires -->
:: > 	<br />
:: > </div>
:: > <div align="center"><input type='submit' name='submit' value="Valider
les
:: > informations de l'étape 3" >
:: > &nbsp;<input type='reset' name='RAZ' value='Annuler' ></div>
:: > </form>
:: >
:: > </body>
:: > </html>
:: >
:: > And I have the second script named upload.jsp which must parse the
request
:: > and upload file.
:: > There is also in this script another form which permits to redirect the
:: page
:: > towards the testUpload.jsp with the hidden field nbPiecesJointes minus
1.
:: > Here is the code of upload.jsp:
:: > <%@ page import="modula.*" %>
:: > <%@ page import="java.io.*, java.util.*, org.w3c.dom.*,
:: > org.apache.commons.fileupload.*" %>
:: > <%
:: > System.out.println("******* upload.jsp *******");
:: > // PieceJointeProposition pjprop = new PieceJointeProposition
:: > (beanUser.cnDBConn);
:: > String fileNameInServer;
:: > int  yourMaxMemorySize = 1024 * 1024 * 8;
:: > int  yourMaxRequestSize = 1024 * 1024 * 8;
:: > int nbPiecesJointes = -1;
:: > String yourTempDirectory = "c:\\temp\\";
:: >
:: > fileNameInServer="";
:: >
:: > DiskFileUpload upload = new DiskFileUpload();
:: >
:: >
:: > // Set upload parameters
:: > upload.setSizeThreshold(yourMaxMemorySize);
:: > upload.setSizeMax(yourMaxRequestSize);
:: > upload.setRepositoryPath(yourTempDirectory);
:: >
:: > // Parse the request
:: > System.out.println("Parse the request");
:: >
:: >
:: > // Parse the request
:: > List items = null;
:: > try {
:: > 	items = upload.parseRequest(request, yourMaxMemorySize,
:: yourMaxRequestSize,
:: > yourTempDirectory);
:: > } catch (FileUploadException e) {
:: > 	//e.printStackTrace();
:: > 	//throw new IOException("could not be uploaded");
:: > 	out.write("le fichier ne peut pas être chargé : fichier trop
:: > volumineux<BR>");
:: > 	out.write("taille maximale autorisée : " + yourMaxRequestSize /
:: (1024 *
:: > 1024) + "Mo");
:: > 	return;
:: > }
:: >
:: > // Process the uploaded items
:: > Iterator iter = items.iterator();
:: > while (iter.hasNext())
:: > {
:: >     FileItem item = (FileItem) iter.next();
:: >
:: >     System.out.println("get item");
:: >
:: >     if (item.isFormField()) {
:: >     	out.write("<br />****** form field ********<br />");
:: >     	//out.write(item.getContentType());
:: >     	out.write(item.getFieldName());
:: >     	out.write(item.getString());
:: >     	if (item.getFieldName().equals("nbPiecesJointes"))
:: >     	{
:: >     		nbPiecesJointes =
Integer.parseInt(item.getString());
:: >     		nbPiecesJointes--;
:: >     		out.write("Modification nbPiecesJointes = " +
:: nbPiecesJointes);
:: >     	}
:: >     	if (item.getFieldName().equals("typeFile"))
:: >     	{
:: >     		int typeFile = -1;
:: >     		if (item.getString().equals(""))
:: >     		{
:: >     			out.write("ERREUR : aucun type choisi");
:: >     		}
:: >     		else
:: >     		{
:: >     			typeFile =
Integer.parseInt(item.getString());
:: >     			out.write("champ typeFile = " + typeFile);
:: >     		}
:: >     	}
:: >     	out.write("<br />****** /form field ********<br />");
:: >     } else {
:: >     	out.write("<br />****** file field ********<br />");
:: > 	    String fieldName = item.getFieldName();
:: > 	    String pathfileName = item.getName();
:: > 	    String fileName ;
:: > 	    String sPrompt ;
:: > 	    String contentType = item.getContentType();
:: > 	    boolean isInMemory = item.isInMemory();
:: > 	    long sizeInBytes = item.getSize();
:: >
:: > 	    String[] parts = pathfileName.split("[\\\\/]");
:: > 	    fileName = parts[parts.length-1];
:: >
:: > 	    sPrompt = "fieldName=" + fieldName + "<BR>"
:: > 		 + "fileName=" + fileName + "<BR>"
:: > 	    	 + "pathfileName=" + pathfileName + "<BR>"
:: > 	     	 + "contentType =" + contentType + "<BR>"
:: > 	    	 + "isInMemory =" + isInMemory + "<BR>"
:: > 	         + "sizeInBytes =" + sizeInBytes + "<BR>";
:: >
:: > 	    System.out.println(sPrompt);
:: > 	    out.write(sPrompt);
:: >
:: > 	    InputStream uploadedStream ;
:: >
:: > 	    String pathFileNameCrypted = "";
:: > 	    File uploadedFile = new File(yourTempDirectory + fileName);
:: > 	    //item.write(uploadedFile);
:: >
:: > 	    // écrit le fichier dans le repertoire temporaire
:: > 	    //uploadedStream = item.getInputStream();
:: > 	    item.write(uploadedFile );
:: > 	    item.delete();
:: > 		out.write("Fichier uploadé");
:: > 		/*
:: > 		FileWriter fw =  new FileWriter (uploadedFile );
:: > 	    fw.write(uploadedStream);
:: > 		fw.flush();
:: > 		fw.close();
:: > 		*/
:: >
:: > 		// chiffrer le fichier
:: > 		//pathFileNameCrypted = GnuPG.encrypt(fileName
:: ,yourTempDirectory ,
:: > "F5A1C6A7");
:: >
:: > 		//sPrompt = "fileName =" + fileName + "<BR>"
:: > 		//	+ "pathFileNameCrypted =" + pathFileNameCrypted +
:: "<BR>";
:: >
:: > 		//System.out.println(sPrompt);
:: > 		//out.write(sPrompt);
:: >
:: > 		// monter le fichier dans la base
:: > 		out.write("<br />****** /file field ********<br />");
:: >     }
:: > }
:: > %>
:: > <form name="upload" method="post" action="testUpload.jsp?nb=<%=
:: > nbPiecesJointes %>" >
:: > <input type="submit" value="Continuer" />
:: > </form>
:: > <%
:: > System.out.println("****** /upload.jsp *******");
:: > //response.sendRedirect("vide.html");
:: > %>
:: >
:: > So I am chaining the different scripts but I have a problem with the
chain
:: > since there is an exception when I attempt to upload big files ( but
size
:: <
:: > 8 Mo)
:: > For exemple I upload a file of 6787600 bytes with no problems but when
I
:: > want upload a file for the second loop I have an exception error with
the
:: > context. I don't understand the problem. I suspect a problem of
buffering
:: > but I don't know what is it...
:: >
:: > If someone could help me to fix my loop, I would be very interested...
:: >
:: > Thanks to all
:: >
:: > Florian Kiebel
:: > ---
:: > Outgoing mail is certified Virus Free.
:: > Checked by AVG anti-virus system (http://www.grisoft.com).
:: > Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
:: >
:: >
:: >
:: > ---------------------------------------------------------------------
:: > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
:: > For additional commands, e-mail: commons-user-help@jakarta.apache.org
:: >
:: >
:: 
:: ---------------------------------------------------------------------
:: To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
:: For additional commands, e-mail: commons-user-help@jakarta.apache.org
:: 
:: 
:: ---------------------------------------------------------------------
:: To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
:: For additional commands, e-mail: commons-user-help@jakarta.apache.org




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


RE: [FileUpload] Problems with chaining uploading

Posted by Florian Kiebel <fl...@matamore.com>.
Hello Martin,

I am sorry to use jsp pages but perhaps (surely) it's a silly question, the
jsp pages aren't compiled into servlet? Perhaps I didn't correctly
understand the principles of jsp and servlets...

But why does it work correctly with one file and not the other?
Actually, I think Commons-Fileupload lacks of documentation... There is no
example of use of this package or I didn't found them...

Do you know any links which permits me to solve my problem of servlet?
(Links with complete examples like servlet code and/or form code)

And other question, I didn't understand your last reported problem. Does it
mean I must use a "response.sendRedirect()" instead of a
"request.sendRedirect()" ?

I hope you could help me, as you surely guess I am a newbie in the jsp and
servlet world so I try to learn a maximum of things.

Nevertheless, I thank you for your time...

Florian Kiebel

-----Message d'origine-----
De : Martin Cooper [mailto:martinc@apache.org] 
Envoyé : lundi 31 mai 2004 19:28
À : Jakarta Commons Users List
Objet : Re: [FileUpload] Problems with chaining uploading

Why, oh why, do so many people insist on using JSP pages for binary
request handling that should be in a servlet...

Anyway, I see two related problems (apart from the fact that the code
should be in a servlet).

1) Redirecting POST requests is generally disallowed, and is specifically
disallowed by the HTTP spec without explicit confirmation from the end
user.

2) You are writing to the response, and then redirecting the request. You
can't do that - a redirect tells the browser to make a separate request
for the subsequent page, and having anything committed in the response
prior to doing a redirect is pretty much guaranteed to cause problems.

--
Martin Cooper


On Mon, 31 May 2004, Florian Kiebel wrote:

> Hello!
>
> I have some problems with FileUpload. So I have two jsp scripts.
> First one is the form script testUpload.jsp... There are an hidden field,
a
> form field and a file field. Here is the code: (my apologizes for the
> comment in French)
>
> <%@ page language="java" %>
> <%@ page import="modula.*" %>
> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
> <html>
> <head>
> <title>Lomboz JSP</title>
> </head>
> <body bgcolor="#FFFFFF">
>
> <form name="upload" method="post" action="upload.jsp"
> enctype="multipart/form-data" >
> <input type="hidden" name="nbPiecesJointes" value="<%=
> request.getParameter("nb") %>">
> <div style="text-align:center; width : 99%;">
> 	<!-- Titre de la page -->
> 	<div class="titre">.: CREER UN MARCHE - ETAPE 3b :.</div>
> 	<!-- /Titre de la page -->
> 	<br />
> 	<!-- Rappel infos administratives -->
> 	<!-- /Rappel infos administratives -->
> 	<br />
> 	<!-- Gestion des pièces jointes obligatoires -->
> 	<div class="result">
> 		<div>
> 			<table width="100%" cellpadding="0" cellspacing="0">
> 				<tr>
> 					<td class="titre_gris">.: Piece
Jointe :.</td>
> 					<td class="titre_gris"
style="text-align:right;">
> 					<strong>Reste : <%=
request.getParameter("nb") %></strong></td>
> 				</tr>
> 			</table>
> 		</div>
> 		<div class="contenu">
> 			 <table width="100%" cellpadding="0"
cellspacing="0">
> 				<tr>
> 					<td style="width : 50% ; text-align
: right;">
> <%
> 	MarchePieceJointeType[] listeTypes =
> MarchePieceJointeType.getAllMarchePieceJointeType();
> %>
> 						<select name="typeFile">
> 							<option> Choisissez
un type de pièce jointe </option>
> <%
> 		for (int j = 0; j < listeTypes.length; j++)
> 		{
> %>
> 							<option value="<%=
listeTypes[j].getIdType() %>">
> 							<%=
listeTypes[j].getTypeDocument() %></option>
> <%
> 		}
> %>
> 						</select>
> 					</td>
> 					<td style="text-align : left;">
> 					<input type="file" name="file">
> 					</td>
> 				</tr>
> 			</table>
> 		</div>
> 	</div>
> 	<!-- /Gestion des pièces jointes obligatoires -->
> 	<br />
> </div>
> <div align="center"><input type='submit' name='submit' value="Valider les
> informations de l'étape 3" >
> &nbsp;<input type='reset' name='RAZ' value='Annuler' ></div>
> </form>
>
> </body>
> </html>
>
> And I have the second script named upload.jsp which must parse the request
> and upload file.
> There is also in this script another form which permits to redirect the
page
> towards the testUpload.jsp with the hidden field nbPiecesJointes minus 1.
> Here is the code of upload.jsp:
> <%@ page import="modula.*" %>
> <%@ page import="java.io.*, java.util.*, org.w3c.dom.*,
> org.apache.commons.fileupload.*" %>
> <%
> System.out.println("******* upload.jsp *******");
> // PieceJointeProposition pjprop = new PieceJointeProposition
> (beanUser.cnDBConn);
> String fileNameInServer;
> int  yourMaxMemorySize = 1024 * 1024 * 8;
> int  yourMaxRequestSize = 1024 * 1024 * 8;
> int nbPiecesJointes = -1;
> String yourTempDirectory = "c:\\temp\\";
>
> fileNameInServer="";
>
> DiskFileUpload upload = new DiskFileUpload();
>
>
> // Set upload parameters
> upload.setSizeThreshold(yourMaxMemorySize);
> upload.setSizeMax(yourMaxRequestSize);
> upload.setRepositoryPath(yourTempDirectory);
>
> // Parse the request
> System.out.println("Parse the request");
>
>
> // Parse the request
> List items = null;
> try {
> 	items = upload.parseRequest(request, yourMaxMemorySize,
yourMaxRequestSize,
> yourTempDirectory);
> } catch (FileUploadException e) {
> 	//e.printStackTrace();
> 	//throw new IOException("could not be uploaded");
> 	out.write("le fichier ne peut pas être chargé : fichier trop
> volumineux<BR>");
> 	out.write("taille maximale autorisée : " + yourMaxRequestSize /
(1024 *
> 1024) + "Mo");
> 	return;
> }
>
> // Process the uploaded items
> Iterator iter = items.iterator();
> while (iter.hasNext())
> {
>     FileItem item = (FileItem) iter.next();
>
>     System.out.println("get item");
>
>     if (item.isFormField()) {
>     	out.write("<br />****** form field ********<br />");
>     	//out.write(item.getContentType());
>     	out.write(item.getFieldName());
>     	out.write(item.getString());
>     	if (item.getFieldName().equals("nbPiecesJointes"))
>     	{
>     		nbPiecesJointes = Integer.parseInt(item.getString());
>     		nbPiecesJointes--;
>     		out.write("Modification nbPiecesJointes = " +
nbPiecesJointes);
>     	}
>     	if (item.getFieldName().equals("typeFile"))
>     	{
>     		int typeFile = -1;
>     		if (item.getString().equals(""))
>     		{
>     			out.write("ERREUR : aucun type choisi");
>     		}
>     		else
>     		{
>     			typeFile = Integer.parseInt(item.getString());
>     			out.write("champ typeFile = " + typeFile);
>     		}
>     	}
>     	out.write("<br />****** /form field ********<br />");
>     } else {
>     	out.write("<br />****** file field ********<br />");
> 	    String fieldName = item.getFieldName();
> 	    String pathfileName = item.getName();
> 	    String fileName ;
> 	    String sPrompt ;
> 	    String contentType = item.getContentType();
> 	    boolean isInMemory = item.isInMemory();
> 	    long sizeInBytes = item.getSize();
>
> 	    String[] parts = pathfileName.split("[\\\\/]");
> 	    fileName = parts[parts.length-1];
>
> 	    sPrompt = "fieldName=" + fieldName + "<BR>"
> 		 + "fileName=" + fileName + "<BR>"
> 	    	 + "pathfileName=" + pathfileName + "<BR>"
> 	     	 + "contentType =" + contentType + "<BR>"
> 	    	 + "isInMemory =" + isInMemory + "<BR>"
> 	         + "sizeInBytes =" + sizeInBytes + "<BR>";
>
> 	    System.out.println(sPrompt);
> 	    out.write(sPrompt);
>
> 	    InputStream uploadedStream ;
>
> 	    String pathFileNameCrypted = "";
> 	    File uploadedFile = new File(yourTempDirectory + fileName);
> 	    //item.write(uploadedFile);
>
> 	    // écrit le fichier dans le repertoire temporaire
> 	    //uploadedStream = item.getInputStream();
> 	    item.write(uploadedFile );
> 	    item.delete();
> 		out.write("Fichier uploadé");
> 		/*
> 		FileWriter fw =  new FileWriter (uploadedFile );
> 	    fw.write(uploadedStream);
> 		fw.flush();
> 		fw.close();
> 		*/
>
> 		// chiffrer le fichier
> 		//pathFileNameCrypted = GnuPG.encrypt(fileName
,yourTempDirectory ,
> "F5A1C6A7");
>
> 		//sPrompt = "fileName =" + fileName + "<BR>"
> 		//	+ "pathFileNameCrypted =" + pathFileNameCrypted +
"<BR>";
>
> 		//System.out.println(sPrompt);
> 		//out.write(sPrompt);
>
> 		// monter le fichier dans la base
> 		out.write("<br />****** /file field ********<br />");
>     }
> }
> %>
> <form name="upload" method="post" action="testUpload.jsp?nb=<%=
> nbPiecesJointes %>" >
> <input type="submit" value="Continuer" />
> </form>
> <%
> System.out.println("****** /upload.jsp *******");
> //response.sendRedirect("vide.html");
> %>
>
> So I am chaining the different scripts but I have a problem with the chain
> since there is an exception when I attempt to upload big files ( but size
<
> 8 Mo)
> For exemple I upload a file of 6787600 bytes with no problems but when I
> want upload a file for the second loop I have an exception error with the
> context. I don't understand the problem. I suspect a problem of buffering
> but I don't know what is it...
>
> If someone could help me to fix my loop, I would be very interested...
>
> Thanks to all
>
> Florian Kiebel
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

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


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


Re: [FileUpload] Problems with chaining uploading

Posted by Martin Cooper <ma...@apache.org>.
Why, oh why, do so many people insist on using JSP pages for binary
request handling that should be in a servlet...

Anyway, I see two related problems (apart from the fact that the code
should be in a servlet).

1) Redirecting POST requests is generally disallowed, and is specifically
disallowed by the HTTP spec without explicit confirmation from the end
user.

2) You are writing to the response, and then redirecting the request. You
can't do that - a redirect tells the browser to make a separate request
for the subsequent page, and having anything committed in the response
prior to doing a redirect is pretty much guaranteed to cause problems.

--
Martin Cooper


On Mon, 31 May 2004, Florian Kiebel wrote:

> Hello!
>
> I have some problems with FileUpload. So I have two jsp scripts.
> First one is the form script testUpload.jsp... There are an hidden field, a
> form field and a file field. Here is the code: (my apologizes for the
> comment in French)
>
> <%@ page language="java" %>
> <%@ page import="modula.*" %>
> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
> <html>
> <head>
> <title>Lomboz JSP</title>
> </head>
> <body bgcolor="#FFFFFF">
>
> <form name="upload" method="post" action="upload.jsp"
> enctype="multipart/form-data" >
> <input type="hidden" name="nbPiecesJointes" value="<%=
> request.getParameter("nb") %>">
> <div style="text-align:center; width : 99%;">
> 	<!-- Titre de la page -->
> 	<div class="titre">.: CREER UN MARCHE - ETAPE 3b :.</div>
> 	<!-- /Titre de la page -->
> 	<br />
> 	<!-- Rappel infos administratives -->
> 	<!-- /Rappel infos administratives -->
> 	<br />
> 	<!-- Gestion des pièces jointes obligatoires -->
> 	<div class="result">
> 		<div>
> 			<table width="100%" cellpadding="0" cellspacing="0">
> 				<tr>
> 					<td class="titre_gris">.: Piece Jointe :.</td>
> 					<td class="titre_gris" style="text-align:right;">
> 					<strong>Reste : <%= request.getParameter("nb") %></strong></td>
> 				</tr>
> 			</table>
> 		</div>
> 		<div class="contenu">
> 			 <table width="100%" cellpadding="0" cellspacing="0">
> 				<tr>
> 					<td style="width : 50% ; text-align : right;">
> <%
> 	MarchePieceJointeType[] listeTypes =
> MarchePieceJointeType.getAllMarchePieceJointeType();
> %>
> 						<select name="typeFile">
> 							<option> Choisissez un type de pièce jointe </option>
> <%
> 		for (int j = 0; j < listeTypes.length; j++)
> 		{
> %>
> 							<option value="<%= listeTypes[j].getIdType() %>">
> 							<%= listeTypes[j].getTypeDocument() %></option>
> <%
> 		}
> %>
> 						</select>
> 					</td>
> 					<td style="text-align : left;">
> 					<input type="file" name="file">
> 					</td>
> 				</tr>
> 			</table>
> 		</div>
> 	</div>
> 	<!-- /Gestion des pièces jointes obligatoires -->
> 	<br />
> </div>
> <div align="center"><input type='submit' name='submit' value="Valider les
> informations de l'étape 3" >
> &nbsp;<input type='reset' name='RAZ' value='Annuler' ></div>
> </form>
>
> </body>
> </html>
>
> And I have the second script named upload.jsp which must parse the request
> and upload file.
> There is also in this script another form which permits to redirect the page
> towards the testUpload.jsp with the hidden field nbPiecesJointes minus 1.
> Here is the code of upload.jsp:
> <%@ page import="modula.*" %>
> <%@ page import="java.io.*, java.util.*, org.w3c.dom.*,
> org.apache.commons.fileupload.*" %>
> <%
> System.out.println("******* upload.jsp *******");
> // PieceJointeProposition pjprop = new PieceJointeProposition
> (beanUser.cnDBConn);
> String fileNameInServer;
> int  yourMaxMemorySize = 1024 * 1024 * 8;
> int  yourMaxRequestSize = 1024 * 1024 * 8;
> int nbPiecesJointes = -1;
> String yourTempDirectory = "c:\\temp\\";
>
> fileNameInServer="";
>
> DiskFileUpload upload = new DiskFileUpload();
>
>
> // Set upload parameters
> upload.setSizeThreshold(yourMaxMemorySize);
> upload.setSizeMax(yourMaxRequestSize);
> upload.setRepositoryPath(yourTempDirectory);
>
> // Parse the request
> System.out.println("Parse the request");
>
>
> // Parse the request
> List items = null;
> try {
> 	items = upload.parseRequest(request, yourMaxMemorySize, yourMaxRequestSize,
> yourTempDirectory);
> } catch (FileUploadException e) {
> 	//e.printStackTrace();
> 	//throw new IOException("could not be uploaded");
> 	out.write("le fichier ne peut pas être chargé : fichier trop
> volumineux<BR>");
> 	out.write("taille maximale autorisée : " + yourMaxRequestSize / (1024 *
> 1024) + "Mo");
> 	return;
> }
>
> // Process the uploaded items
> Iterator iter = items.iterator();
> while (iter.hasNext())
> {
>     FileItem item = (FileItem) iter.next();
>
>     System.out.println("get item");
>
>     if (item.isFormField()) {
>     	out.write("<br />****** form field ********<br />");
>     	//out.write(item.getContentType());
>     	out.write(item.getFieldName());
>     	out.write(item.getString());
>     	if (item.getFieldName().equals("nbPiecesJointes"))
>     	{
>     		nbPiecesJointes = Integer.parseInt(item.getString());
>     		nbPiecesJointes--;
>     		out.write("Modification nbPiecesJointes = " + nbPiecesJointes);
>     	}
>     	if (item.getFieldName().equals("typeFile"))
>     	{
>     		int typeFile = -1;
>     		if (item.getString().equals(""))
>     		{
>     			out.write("ERREUR : aucun type choisi");
>     		}
>     		else
>     		{
>     			typeFile = Integer.parseInt(item.getString());
>     			out.write("champ typeFile = " + typeFile);
>     		}
>     	}
>     	out.write("<br />****** /form field ********<br />");
>     } else {
>     	out.write("<br />****** file field ********<br />");
> 	    String fieldName = item.getFieldName();
> 	    String pathfileName = item.getName();
> 	    String fileName ;
> 	    String sPrompt ;
> 	    String contentType = item.getContentType();
> 	    boolean isInMemory = item.isInMemory();
> 	    long sizeInBytes = item.getSize();
>
> 	    String[] parts = pathfileName.split("[\\\\/]");
> 	    fileName = parts[parts.length-1];
>
> 	    sPrompt = "fieldName=" + fieldName + "<BR>"
> 		 + "fileName=" + fileName + "<BR>"
> 	    	 + "pathfileName=" + pathfileName + "<BR>"
> 	     	 + "contentType =" + contentType + "<BR>"
> 	    	 + "isInMemory =" + isInMemory + "<BR>"
> 	         + "sizeInBytes =" + sizeInBytes + "<BR>";
>
> 	    System.out.println(sPrompt);
> 	    out.write(sPrompt);
>
> 	    InputStream uploadedStream ;
>
> 	    String pathFileNameCrypted = "";
> 	    File uploadedFile = new File(yourTempDirectory + fileName);
> 	    //item.write(uploadedFile);
>
> 	    // écrit le fichier dans le repertoire temporaire
> 	    //uploadedStream = item.getInputStream();
> 	    item.write(uploadedFile );
> 	    item.delete();
> 		out.write("Fichier uploadé");
> 		/*
> 		FileWriter fw =  new FileWriter (uploadedFile );
> 	    fw.write(uploadedStream);
> 		fw.flush();
> 		fw.close();
> 		*/
>
> 		// chiffrer le fichier
> 		//pathFileNameCrypted = GnuPG.encrypt(fileName ,yourTempDirectory ,
> "F5A1C6A7");
>
> 		//sPrompt = "fileName =" + fileName + "<BR>"
> 		//	+ "pathFileNameCrypted =" + pathFileNameCrypted + "<BR>";
>
> 		//System.out.println(sPrompt);
> 		//out.write(sPrompt);
>
> 		// monter le fichier dans la base
> 		out.write("<br />****** /file field ********<br />");
>     }
> }
> %>
> <form name="upload" method="post" action="testUpload.jsp?nb=<%=
> nbPiecesJointes %>" >
> <input type="submit" value="Continuer" />
> </form>
> <%
> System.out.println("****** /upload.jsp *******");
> //response.sendRedirect("vide.html");
> %>
>
> So I am chaining the different scripts but I have a problem with the chain
> since there is an exception when I attempt to upload big files ( but size <
> 8 Mo)
> For exemple I upload a file of 6787600 bytes with no problems but when I
> want upload a file for the second loop I have an exception error with the
> context. I don't understand the problem. I suspect a problem of buffering
> but I don't know what is it...
>
> If someone could help me to fix my loop, I would be very interested...
>
> Thanks to all
>
> Florian Kiebel
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

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