commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Florian Kiebel" <florian.kie...@matamore.com>
Subject [FileUpload] Problems with chaining uploading
Date Mon, 31 May 2004 17:01:31 GMT
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


Mime
View raw message