Derek
Below is the code:
/**
* uploadFile.java
*
* Created on December 11, 2002, 11:40 PM
*
*
* @author Design_DEMON
* @version 1.0
* Based on base classes from org.apache.commons.fileupload
* Explenation of Strings:<br>
* public String folder:: This string contains the information of your local
* directory for testing your application locally.
* <br>
* public String tempFolder:: This is the location of your temp folder when
* testing the application locally.
* <br>
* public String serverFolder:: Uncomment this line when transfering the
files
* to the server. This is the complete string from the server root.
* <br>
* public String serverTemp:: Uncomment this line when transfering to the
server.
* Tis should point to your temp directory on the server from the server
root.
*/
package volume4.sharelite.fileManager;
import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.commons.fileupload.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class uploadFile extends HttpServlet {
public String folder = "/Program Files/Apache Tomcat
4.0/webapps/PersonnelFinance/fileShare/fileExchange/files/";
public String temp = "/temp";
//public String serverFolder =
"/var/www/vhosts/personnelfinance/webapps/Persfin/fileShare/fileExchange/fil
es/";
//public String serverTemp =
"/var/www/vhosts/personnelfinance/webapps/Persfin/temp/";
public String DRIVER, URL, USER, PASS, recipient, fieldName, fieldData,
fileName, fileFieldName, comment, filePath;
public void init() throws ServletException {
ServletContext context = getServletContext();
DRIVER = context.getInitParameter("DRIVER");
URL = context.getInitParameter("PFURL");
USER = context.getInitParameter("PFUSER");
PASS = context.getInitParameter("PFPASS");
}
protected void processRequest(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, java.io.IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection con = null;
try {
FileUpload upload = new FileUpload();
upload.setSizeMax(400000000);
upload.setSizeThreshold(4096);
upload.setRepositoryPath(temp);
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
fieldName = item.getFieldName();
if(fieldName.equals("recipient")) {
fieldData = item.getString();
recipient = fieldData;
}
else if(fieldName.equals("comment")) {
fieldName = item.getFieldName();
fieldData = item.getString();
comment = fieldData;
}
System.out.println("recipient: ");
System.out.println(recipient);
System.out.println("comment: ");
System.out.println(comment);
} else {
if(item.getName().equals("")) {
System.out.println("forwarding");
} else {
fileName = item.getName();
fileFieldName = item.getFieldName();
StringTokenizer tokenizer = new StringTokenizer(fileName, "\\,
:, /");
int amount = tokenizer.countTokens();
for (int i = 0; i < amount -1; i++) {
tokenizer.nextToken();
}
String currentFile = tokenizer.nextToken();
filePath = folder + currentFile;
item.getInputStream();
item.write(folder + currentFile);
item.delete();
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PASS);
PreparedStatement stmt = con.prepareStatement
("INSERT INTO fileexchange (fileName, fileURI, comments, toUser)
" +
"VALUES (?, ?, ?, ?)"
);
stmt.setString(1, currentFile);
stmt.setString(2, filePath);
stmt.setString(3, comment);
stmt.setString(4, recipient);
int result = stmt.executeUpdate();
}
}
}
res.sendRedirect("file_uploaded.jsp");
} catch(FileUploadException fue) {
fue.printStackTrace();
out.println("There was and error when reading and writing the file
to the server.");
} catch(Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException {
processRequest(req, res);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException {
processRequest(req, res);
}
}
Kind Regards �
Schalk Neethling �
Volume4.Development.Multimedia.Branding
.emotionalize.conceptualize.visualize.realize
Tel: +27125468436
Fax: +27125468436
email: [EMAIL PROTECTED]
url: www.volume4.co.za
----- Original Message -----
From: "Durham David Cntr 805CSS/SCBE" <[EMAIL PROTECTED]>
To: "Jakarta Commons Users List" <[EMAIL PROTECTED]>
Sent: Thursday, February 13, 2003 11:42 PM
Subject: RE: [FileUpload] basic question
> Why not just post it in the email's text, I'm interested in this as well.
>
> > -----Original Message-----
> > From: Schalk [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, February 13, 2003 3:41 PM
> > To: Jakarta Commons Users List
> > Subject: Re: [FileUpload] basic question
> >
> >
> > Derek
> >
> > Give me your email and I will send it there
> >
> >
> > Kind Regards �
> > Schalk Neethling �
> > Volume4.Development.Multimedia.Branding
> > .emotionalize.conceptualize.visualize.realize
> > Tel: +27125468436
> > Fax: +27125468436
> > email: [EMAIL PROTECTED]
> > url: www.volume4.co.za
> > ----- Original Message -----
> > From: "Derek A. Bodin" <[EMAIL PROTECTED]>
> > To: "'Jakarta Commons Users List'" <[EMAIL PROTECTED]>
> > Sent: Thursday, February 13, 2003 11:37 PM
> > Subject: RE: [FileUpload] basic question
> >
> >
> > > The file didn't make it through, thanks for the quick response.
> > >
> > > -Derek
> > >
> > > -----Original Message-----
> > > From: Schalk [mailto:[EMAIL PROTECTED]]
> > > Sent: Thursday, February 13, 2003 3:34 PM
> > > To: Jakarta Commons Users List
> > > Subject: Re: [FileUpload] basic question
> > >
> > > Derek
> > >
> > > Try using the attached java file.
> > >
> > > Kind Regards >
> > > Schalk Neethling >
> > > Volume4.Development.Multimedia.Branding
> > > .emotionalize.conceptualize.visualize.realize
> > > Tel: +27125468436
> > > Fax: +27125468436
> > > email: [EMAIL PROTECTED]
> > > url: www.volume4.co.za
> > > ----- Original Message -----
> > > From: "Derek A. Bodin" <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Thursday, February 13, 2003 11:16 PM
> > > Subject: [FileUpload] basic question
> > >
> > >
> > > > Hi, I am using FileUpload with Tomcat 4.1.18. I am
> > sending my request
> > > > with all of the correct fields, method=post,
> > > > enctype="multipart/form-data". FileUpload parses the request fine
> > > > (without throwing an exception) but returns an empty
> > list. Is there
> > > > something I am missing? I read the API's and am modeling
> > my test on
> > > the
> > > > example shown in the package description. Any help would be
> > > > appreciated.
> > > >
> > > > Thank You,
> > > >
> > > > -Derek
> > > >
> > > >
> > > >
> > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> > [EMAIL PROTECTED]
> > > > For additional commands, e-mail:
> > [EMAIL PROTECTED]
> > > >
> > >
> > >
> > >
> > >
> > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail:
> > [EMAIL PROTECTED]
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]