Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@jakarta.apache.org Received: (qmail 6420 invoked by uid 500); 5 Jun 2001 12:10:31 -0000 Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk Reply-To: tomcat-user@jakarta.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 4433 invoked from network); 5 Jun 2001 12:08:02 -0000 Received: from unknown (HELO relay-mail.siemens.pt) (194.145.59.101) by h31.sny.collab.net with SMTP; 5 Jun 2001 12:08:02 -0000 Received: from sieinter.siemens.pt (relay.siemens.pt [194.145.59.6]) by relay-mail.siemens.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id MCZT070R; Tue, 5 Jun 2001 12:33:32 +0100 Received: from lisi007a.siemens.pt by sieinter.siemens.pt via smtpd (for [194.145.59.101]) with SMTP; 5 Jun 2001 12:04:32 UT Received: by lisi007a.siemens.pt with Internet Mail Service (5.5.2653.19) id ; Tue, 5 Jun 2001 12:28:03 +0100 Received: from YBRP154326 ([141.29.144.87]) by siepor43.siemens.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id MDBRDA6T; Tue, 5 Jun 2001 12:27:49 +0100 Message-ID: <007901c0edb3$7d02ac70$57901d8d@siemens.pt> From: =?iso-8859-1?Q?Jo=E3o_Folha?= To: Subject: Fw: binary file upload fail Date: Tue, 5 Jun 2001 12:34:31 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0075_01C0EDBB.DE77BBF0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N This is a multi-part message in MIME format. ------=_NextPart_000_0075_01C0EDBB.DE77BBF0 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0076_01C0EDBB.DE77BBF0" ------=_NextPart_001_0076_01C0EDBB.DE77BBF0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable =20 ----- Original Message -----=20 From: Jo=E3o Folha=20 To: tomcat-user@jakarta.apache.org =20 Sent: Monday, June 04, 2001 8:06 PM Subject: Fw: binary file upload fail =20 ----- Original Message -----=20 From: Jo=E3o Folha=20 To: tomcat-user@jakarta.apache.org =20 Sent: Monday, June 04, 2001 8:06 PM Subject: Fw: binary file upload fail =20 ----- Original Message -----=20 From: Jo=E3o Folha=20 To: tomcat-user@jakarta.apache.org =20 Sent: Monday, June 04, 2001 6:17 PM Subject: binary file upload fail Hi there, =20 I am trying to upload a file from the client to the server. But the binary files fail and the text files don=B4t fail. I use tomcat 3.2.1 with apj12. The bean and the jsp file i use are in attachment... =20 regards =20 jfolha =20 ------=_NextPart_001_0076_01C0EDBB.DE77BBF0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
 

 

 

Hi there,
 
I am trying to upload a file from the = client to the=20 server.
But the binary files fail and the text = files don=B4t=20 fail.
I use tomcat 3.2.1 with = apj12.
The bean and the jsp file i use are in = attachment...
 
regards
 
jfolha
 
------=_NextPart_001_0076_01C0EDBB.DE77BBF0-- ------=_NextPart_000_0075_01C0EDBB.DE77BBF0 Content-Type: application/octet-stream; name="doc_entrega_autor.jsp" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="doc_entrega_autor.jsp" =0A= =0A= =0A= =0A= <%@ page language=3D"java" %>=0A= <%@ page import=3D"java.lang.*, java.util.*,java.io.*" %>=0A= =0A= =0A= =0A= =0A= Entregar Documento=0A= =0A= =0A= =0A= =0A= =0A= <%=0A= String nc =3D (String) session.getAttribute("numero_colaborador");=0A= String email =3D (String) session.getAttribute("email");=0A= String nid =3D (String) request.getParameter("document");=0A= String entregar =3D (String) request.getParameter("entregar");=0A= %>=0A= =0A= =0A=
=0A= ">=0A= ">=0A= =0A= =0A= =0A= <%=0A= if(email!=3D null){=0A= %>=0A= =0A= =0A= =0A= =0A= <%=0A= }=0A= else {=0A= %>=0A= =0A= =0A= =0A= =0A= <%=0A= }=0A= %>=0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A= =0A=
From:<%=3D email %>">
From
To:Adminstrador
Assunto:Entrega nid:<%=3Dnid %> do nc:<%=3Dnc %> do colaborador:<%=3Dnc %>">
=0A=
=0A= =0A= =0A= =0A= =0A= ------=_NextPart_000_0075_01C0EDBB.DE77BBF0 Content-Type: application/octet-stream; name="upload.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="upload.java" package cds;=0A= =0A= import javax.servlet.http.HttpServletRequest;=0A= import javax.servlet.ServletInputStream;=0A= import java.util.Dictionary;=0A= import java.util.Hashtable;=0A= import java.io.PrintWriter;=0A= import java.io.PrintStream;=0A= import java.io.OutputStream;=0A= import java.io.BufferedWriter;=0A= import java.io.FileWriter;=0A= import java.io.IOException;=0A= =0A= // ---------------------------------------------------------=0A= // class upload {=0A= // ---------------------------------------------------------=0A= public class upload {=0A= =0A= private String savePath, filepath, filename, contentType;=0A= private Dictionary fields;=0A= private boolean fileUpload=3Dfalse;=0A= private boolean endUpload =3D false;=0A= =0A= // ---------------------------------------------------------=0A= // getEndUpload: name of the upload file=0A= // ---------------------------------------------------------=0A= public boolean getEndUpload() {=0A= return endUpload;=0A= }=0A= // ---------------------------------------------------------=0A= // setEndUpload: name of the upload file=0A= // ---------------------------------------------------------=0A= public void setEndUpload(boolean endupload) {=0A= this.endUpload =3D endupload;=0A= }=0A= =0A= // ---------------------------------------------------------=0A= // getFileUpload: name of the upload file=0A= // ---------------------------------------------------------=0A= public boolean getFileUpload() {=0A= return fileUpload;=0A= }=0A= // ---------------------------------------------------------=0A= // setFileUpload: name of the upload file=0A= // ---------------------------------------------------------=0A= public void setFileUpload(boolean fileupload) {=0A= this.fileUpload =3D fileupload;=0A= }=0A= =0A= // ---------------------------------------------------------=0A= // getFilename: name of the upload file=0A= // ---------------------------------------------------------=0A= public String getFilename() {=0A= return filename;=0A= }=0A= // ---------------------------------------------------------=0A= // getFilepath: complete path of the upload file on the client=0A= // ---------------------------------------------------------=0A= public String getFilepath() {=0A= return filepath;=0A= }=0A= // ---------------------------------------------------------=0A= // setSavePath: where the upload file should be save on the server=0A= // ---------------------------------------------------------=0A= public void setSavePath(String savePath) {=0A= this.savePath =3D savePath;=0A= }=0A= // ---------------------------------------------------------=0A= // getContentType: specifies the type of the upload file content=0A= // ---------------------------------------------------------=0A= public String getContentType() {=0A= return contentType;=0A= }=0A= // ---------------------------------------------------------=0A= // getFieldValue: stores the name/value pairs of HTML form's input = elements=0A= // ---------------------------------------------------------=0A= public String getFieldValue(String fieldName) {=0A= if (fields =3D=3D null || fieldName =3D=3D null)=0A= return null;=0A= return (String) fields.get(fieldName);=0A= }=0A= // ---------------------------------------------------------=0A= // setFilename: name of the upload file=0A= // ---------------------------------------------------------=0A= private void setFilename(String s) {=0A= if (s=3D=3Dnull)=0A= return;=0A= =0A= int pos =3D s.indexOf("filename=3D\"");=0A= if (pos !=3D -1) {=0A= filepath =3D s.substring(pos+10, s.length()-1);=0A= // Windows browsers include the full path on the client=0A= // But Linux/Unix and Mac browsers only send the filename=0A= // test if this is from a Windows browser=0A= pos =3D filepath.lastIndexOf("\\");=0A= if (pos !=3D -1)=0A= filename =3D filepath.substring(pos + 1);=0A= else=0A= filename =3D filepath;=0A= // System.out.println("setfilename:" + filename);=0A= }=0A= }=0A= // ---------------------------------------------------------=0A= // setContentType=0A= // ---------------------------------------------------------=0A= private void setContentType(String s) {=0A= if (s=3D=3Dnull)=0A= return;=0A= =0A= int pos =3D s.indexOf(": ");=0A= if (pos !=3D -1)=0A= contentType =3D s.substring(pos+2, s.length());=0A= System.out.println("contentType:" + contentType);=0A= }=0A= =0A= // ---------------------------------------------------------=0A= // do Upload=0A= // ---------------------------------------------------------=0A= public void doUpload(HttpServletRequest request) throws IOException = {=0A= =0A= ServletInputStream in =3D request.getInputStream();=0A= =0A= byte[] line =3D new byte[128];=0A= =0A= int i=3D0;=0A= =0A= try{=0A= // read the first line of HttpServletRequest objects's content=0A= i =3D in.readLine(line, 0, 128);=0A= =0A= // the first line should be the boundary and its length,=0A= // must be must longer than 3=0A= if (i < 3)=0A= return;=0A= =0A= //-2 discards the newline character=0A= int boundaryLength =3D i - 2;=0A= =0A= // the clean first line=0A= // example: -----------------------------7d15340138=0A= // Read from line start at 0 to boundaryLength=0A= String boundary =3D new String(line, 0, boundaryLength);=0A= //System.out.println("boundary:" + boundary);=0A= =0A= fields =3D new Hashtable();=0A= =0A= while (i !=3D -1) {=0A= =0A= String newLine =3D new String(line, 0, i);=0A= //System.out.println("newLine:" + newLine);=0A= =0A= if (newLine.startsWith("Content-Disposition: form-data; = name=3D\"")) {=0A= =0A= // get the index where the substring "filename=3D\"" ends=0A= if (newLine.indexOf("filename=3D\"") !=3D -1) {=0A= // save the name of the filename=0A= setFilename(new String(line, 0, i-2));=0A= // if this happens Houston we have a problem=0A= if (filename=3D=3Dnull) {=0A= setFileUpload(false);=0A= return;=0A= }=0A= =0A= //From here we get the file=0A= i =3D in.readLine(line, 0, 128);=0A= // first we get the file content-type=0A= setContentType(new String(line, 0, i-2));=0A= i =3D in.readLine(line, 0, 128);=0A= // skip the blank line=0A= i =3D in.readLine(line, 0, 128);=0A= // get the next line=0A= newLine =3D new String(line, 0, i);=0A= =0A= // where to save the file=0A= PrintWriter pw=3D new PrintWriter(new BufferedWriter(=0A= new FileWriter((savePath=3D=3Dnull? "" : savePath) + = filename)));=0A= =0A= // while to get the file=0A= System.out.println("startfile");=0A= while (i !=3D -1 && !newLine.startsWith(boundary)) {=0A= // the problem is the last line of the file content=0A= // contains the new line character.=0A= // So, we need to check if the current line is the last = line.=0A= i =3D in.readLine(line, 0, 128);=0A= // + 4 is eof=0A= if ((i=3D=3DboundaryLength+2 || i=3D=3DboundaryLength+4) && = (new String(line, 0, i).startsWith(boundary))) {=0A= pw.print(newLine.substring(0, newLine.length()-2));=0A= System.out.println("kkkk:" + newLine.substring(0, = newLine.length()-2) + "@");=0A= }=0A= else {=0A= pw.print(newLine);=0A= System.out.println("yyyy:" + newLine + "@");=0A= }=0A= =0A= newLine =3D new String(line, 0, i);=0A= //System.out.println("newline:" + newLine);=0A= }=0A= System.out.println("endfile");=0A= pw.close();=0A= // flag of fileUpload with success=0A= setFileUpload(true);=0A= }=0A= else {=0A= // get the field name=0A= // get the index where the substring "name=3D\"" ends=0A= int pos =3D newLine.indexOf("name=3D\"");=0A= String fieldName =3D newLine.substring(pos+6, = newLine.length()-3);=0A= =0A= // blank line=0A= i =3D in.readLine(line, 0, 128);=0A= i =3D in.readLine(line, 0, 128);=0A= newLine =3D new String(line, 0, i);=0A= StringBuffer fieldValue =3D new StringBuffer(128);=0A= while (i !=3D -1 && !newLine.startsWith(boundary)) {=0A= // The last line of the field contains the new line = character.=0A= // So, we need to check if the current line is the last = line.=0A= i =3D in.readLine(line, 0, 128);=0A= // + 4 is eof=0A= if ((i=3D=3DboundaryLength+2 || i=3D=3DboundaryLength+4) && = (new String(line, 0, i).startsWith(boundary)))=0A= fieldValue.append(newLine.substring(0, = newLine.length()-2));=0A= else=0A= fieldValue.append(newLine);=0A= newLine =3D new String(line, 0, i);=0A= }=0A= System.out.println("fieldValue:" + fieldValue.toString());=0A= fields.put(fieldName, fieldValue.toString());=0A= }=0A= }=0A= i =3D in.readLine(line, 0, 128);=0A= =0A= } // end while=0A= // flag of fileUpload method ends=0A= setEndUpload(true);=0A= }=0A= catch(java.io.IOException ioe){=0A= ioe.printStackTrace();=0A= }=0A= }=0A= =0A= // ---------------------------------------------------------=0A= // END=0A= // ---------------------------------------------------------=0A= =0A= } ------=_NextPart_000_0075_01C0EDBB.DE77BBF0--