Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@jakarta.apache.org Received: (qmail 89386 invoked by uid 500); 19 Jun 2001 15:05:27 -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 89347 invoked from network); 19 Jun 2001 15:05:19 -0000 Received: from unknown (HELO mail1.siemens.pt) (194.145.62.101) by h31.sny.collab.net with SMTP; 19 Jun 2001 15:05:19 -0000 Received: from siepor43.siemens.pt ([141.29.156.127]) by mail1.siemens.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id M90MT76B; Tue, 19 Jun 2001 16:04:31 +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 NFCWMJ5A; Tue, 19 Jun 2001 16:04:49 +0100 Message-ID: <01f001c0f8d2$472d0fd0$57901d8d@siemens.pt> From: =?iso-8859-1?Q?Jo=E3o_Folha?= To: Subject: How to upload a binary file? Date: Tue, 19 Jun 2001 16:12:38 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_01EC_01C0F8DA.A8B2C120" 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_01EC_01C0F8DA.A8B2C120 Content-Type: multipart/alternative; boundary="----=_NextPart_001_01ED_01C0F8DA.A8B2C120" ------=_NextPart_001_01ED_01C0F8DA.A8B2C120 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi there, 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, on NT4.0 The bean and the jsp file i use are in attachment... regards jfolha ------=_NextPart_001_01ED_01C0F8DA.A8B2C120 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, on=20 NT4.0
The bean and the jsp file i use are in=20 attachment...
 
regards
 
jfolha
 
------=_NextPart_001_01ED_01C0F8DA.A8B2C120-- ------=_NextPart_000_01EC_01C0F8DA.A8B2C120 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.FileWriter;=0A= import java.io.PrintWriter;=0A= import java.io.PrintStream;=0A= import java.io.OutputStream;=0A= import java.io.BufferedWriter;=0A= import java.io.FileOutputStream;=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:" + s);=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= // skip the blank line=0A= i =3D in.readLine(line, 0, 128);=0A= // get the next line=0A= i =3D in.readLine(line, 0, 128);=0A= =0A= newLine =3D new String(line, 0, i);=0A= =0A= // where to save the file=0A= PrintWriter pw =3D new PrintWriter(new BufferedWriter(new=0A= 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= //if(newLine.substring(0, newLine.length()-2)!=3Dnull)=0A= pw.print(newLine.substring(0, newLine.length()-2));=0A= pw.flush();=0A= //System.out.println("kkkk:" + newLine.substring(0, = newLine.length()-2) + "@");=0A= }=0A= else {=0A= //if(newLine!=3Dnull)=0A= pw.print(newLine);=0A= pw.flush();=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_01EC_01C0F8DA.A8B2C120 Content-Type: application/octet-stream; name="doc_entrega_autor.jsp" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="doc_entrega_autor.jsp" <%-- ------------------------------------ --%>=0A= <%-- doc_entrega_autor.jsp --%>=0A= <%-- ------------------------------------ --%>=0A= =0A= <%-- ------------------------------------ --%>=0A= <%-- This page list displays a form --%>=0A= <%-- The goal is to send a file with attachment to the administrator --%>=0A= <%-- Recebed parameters: document, entregar --%>=0A= <%-- ------------------------------------ --%>=0A= =0A= <%@ page language=3D"java" contentType=3D"text/html" %>=0A= <%@ page import=3D"java.lang.*, java.util.*,java.io.*" %>=0A= =0A= =0A= =0A= <%-- BASE HREF=3D"http://localhost:8080/public/autor" --%>=0A= Entregar Documento=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 request.getParameter("document");=0A= String entregar =3D request.getParameter("entregar");=0A= %>=0A= =0A= <%-- ------------------------------------ --%>=0A= <%-- Careful: Use ENCTYPE=3D"multipart/form-data" and method=3D"POST" = --%>=0A= <%-- Form parameters: --%>=0A= <%-- document, entregar, from, subject, messageText, Attachment, Send = --%>=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=
From:<%=3D email %>">
From:
To:Adminstrador
Assunto:Entrega nid:<%=3Dnid %> do nc:<%=3Dnc %> do colaborador:<%=3Dnc %>">
=0A=
=0A= =0A= =0A= =0A= <%-- ------------------------------------ --%>=0A= <%-- END FILE --%>=0A= <%-- ------------------------------------ --%>=0A= ------=_NextPart_000_01EC_01C0F8DA.A8B2C120--