Dear All,
This email to letyou know that I have used a sample
code from
Schalk Neethling @ Volume 4. ([EMAIL PROTECTED])
His code shows how to make use of the fileUpload
API. Which I had
some problem with and couldn't find detailed
example.
I must say that it *really* helped me
out.
Maybe you guys could have a look at it and maybe
include it in the package.
I'm sure it could help several people out
there.
The code is attached, with his
explanations.
Best Regards,
Mamoudou Traoré.
|
/** * 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.manageIT.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/files/"; //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); } }
Mamoudou How my uploading process works is as follows: Download the binary version of the fileUpload from the fileUpload site on Jakarta. Download the commons-beanutils.jar (binary distibution) fron the commons section of Jakarta. Place both of these in your lib directory within WEB-INF. The I use a servlet as the "middle tear between two fron end pages. The firts the form used to select the file and the second the file returned to the client when the file has been successfuly uploaded, I call mine file_uploaded.jsp. Place the servlet in your servlet directory and add the following code to your web.xml: <servlet> <servlet-name>uploadFile</servlet-name> <servlet-class>volume4.manageIT.fileManager.uploadFile</servlet-class> </servlet> <servlet-mapping> <servlet-name>uploadFile</servlet-name> <url-pattern>*.uploadFile</url-pattern> </servlet-mapping> The in the form action of your first page set it to file.uploadFile This should do it. Read the comments in the attached .java file to see where to specify your target and temp directories. Hope this helps you. Kind Regards » Schalk Neethling » Volume4.Development.Multimedia.Branding .emotionalize.conceptualize.visualize.realize Tel: +27125468436 Fax: +27125468436 email: [EMAIL PROTECTED] url: www.volume4.co.za
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]