Hi
I have a jsp file where I would like download files to a particular
directory on the server.
I am have form fields, including file upload fields, the content of
which is forwarded to a second jsp where I use MultipartRequest from the
com.oreilly.servlet.* package to download the uploaded files to a
location on the server.
This all works well, but I would like to check first if file(s) with the
same filename already exists in that directory on the server. This can
be done by executing a unix command on the server and getting the
filenames from the directory.
So far so good. However, the moment I use MultipartRequest to get the
names of the files that are uploaded (to compare them with the ones
already in the directory), they are immediately downloaded to the
directory.
So I thought I use MultipartParser first to obtain the filenames - but
the moment I do this, I am using the generic method HttpServlet request
and this means that, when using MultipartParser early in the jsp it
seems I can no longer use MultipartRequest afterwards and vice versa (if
there's a solution to this, please let me know).
This led me to try to make the part that checks existing filenames
agains the filenames in the upload fields into a javaBean - I can then
call the Bean's method that does the checking from my second jsp file
(to which form variables are forwarded). The bean would simply return a
string containing "yes" of "no" depending on whether the filenames to be
stored are unique or not.
However, I have to pass the contents of the fields that contains the
filenames to the bean - which means I have to once more use a request
method to get those values in the first place.
So the problem is that somehow I have to pass on HttpServlet request
values to the bean, and I am not sure how to do this without getting the
values out first with MultiPartParser - which makes it impossible for me
to use MultipartRequest.
The bean is listed in my jsp file as:
<jsp:useBean id="checkupload" scope="session"
class="com.geoinformex.project.checkUpload"/>
and I am calling its method further down in the jsp:
String samefile = checkupload.getdoCheck(BUT HERE I NEED TO PASS IN A
REQUEST!);
I have to pass in the request (parameter of file to be uploaded) to the
bean - have a look at the method below in the bean itself. Note that
without making the bean extend HttpServlet I cannot construct
MultipartParser parser in the Bean's method getdoCheck later on.
Complex problem, I know, but I am sort of hoping someone can set me back
on the right path, as at this stage I am really lost as how to proceed.
Any help, particularly code examples will be greatly appreciated.
Thanks very much.
Hugo
***
Here is the bean:
package com.geoinformex.project;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.*;
import com.oreilly.servlet.multipart.MultipartParser;
public class checkUpload extends HttpServlet{
//String lineOfText ="";
String proj_id_b ="";
String proj_id ="";
//String makedir ="";
int proj_id_count = 0;
Vector proj_id_number = new Vector();
Vector proj_id_base = new Vector();
Vector project_ID_list = new Vector();
Vector existingfiles = new Vector();
String upload1 = "";
String upload2 = "";
String upload3 = "";
Part part;
public String getdoCheck(HttpServletRequest request,HttpServletResponse
response) throws IOException, ServletException {
String samefile = "";
//Trying to get the filenames from the upload fields from my form (but
this fails).
MultipartParser parser = new MultipartParser(request, 10*1024*1024);
while ((part = parser.readNextPart()) != null) {
if (part.isFile()) {
// It's a file part I am after
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
existingfiles.add(fileName);
System.out.println("filename: " + fileName);
}
}
}
// Reading in filenames from the directory.
try {
Process p = Runtime.getRuntime().exec("ls /var/www/projects_data/");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String s = "";
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println ("<br>line of input is: " + s);
StringTokenizer tr = new StringTokenizer(s," ", true );
String t = tr.nextToken();
System.out.println("<br>token is: "+ t);
for (int i = 0; i < existingfiles.size(); i++) {
//Comparing filenames obtained from directory with
those obtained from file input fields.
if (t.equals(existingfiles.get(i).toString())) {
samefile = "yes";
} else {
samefile = "no";
}
}
}
} catch (IOException e) {
System.out.println("exception: ");
e.printStackTrace();
System.exit(-1);
}
return samefile;
}
}
--
Dr Hugo Bouckaert
Systems and Programming Engineer
GeoInformatics Exploration Australia P/L
57 Havelock St
West Perth, WA 6005
PO Box 1675, West Perth 6872
Ph: 61 08 9420 7400
Fax: 61 08 9226 1299
www.geoinformex.com
------------------------------------------------------------------------
This email and any attachments may be confidential or legally
privileged. If you received this message in error or are not the
intended recipient, you should destroy the e-mail message and any
attachments or copies, and you are prohibited from retaining,
distributing, disclosing or using any information contained herein.
Please inform us of the erroneous delivery by return e-mail. Thank you
for your cooperation.
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com