• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Uploading and Verifying an HTML page using a Servlet.

I was just wondering if anyone has needed to do this before.  It is fairly generic, I have just a generic HTML page that submits via "multipart/form-data" to a Java Servlet.  I know that there is no built-in Java classes to deal with multipart, but I am trying with the O'Reilly File Upload package: MultipartRequest and MultipartParser (available at: http://www.servlets.com/resources/com.oreilly.servlet/index.html)  If you would know a better (or alternative) tool then let me know and I will use that instead.

Anyways, I want to have my Servlet take the file that is submitted (which should be an HTML page) and verify that it has a title that isn't "", (ie. between <title></title> tags) and then save the HTML file locally.  I don't imagine that this is too difficult, but any help would be much appreciated.  Oh, and I want to have the title stored in a String so that it can be passed to another method that will put it in a database... thanks in advance.
0
ozy123
Asked:
ozy123
1 Solution
 
tutranCommented:
Is the question on how to upload or just how to parse the <title> tag.  If it's just the <title>, why don't you save it to a file locally first, then open and use BufferedReader readline() method and do String indexOf("<title>") method.


    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);

    String text = br.readLine();
    if (text != null) {
     
         int beginInd = text.indexOf("<title>");
         int endInd = text.indexOf("</title>");
         if (beginInd != -1 && endInd != -1) {
              String title = text.substring(beginInd, endInd);
     
              // Process the tile value        
            }
    }
0
 
ozy123Author Commented:
I suppose tutran's suggested answer is fairly close.  I was interested also if anyone had actually used the O'Reilly File Upload package to see if they could explain how to use it, but I think the following worked for me:

   String line = new String();
   String filePath = "some/path/";

   MultipartRequest multiReq = new MultipartRequest( request, filePath, MAX_FILE_UPLOAD_SIZE, SpecificRenamePolicy /* extends the FileRenamePolicy included with O'Reilly */ );
   Enumeration files = multiReq.getFileNames();
   String nameOfFile = (String) files.nextElement();

/* Since there is only one file, and it should be rename to something different than the name of the file they uploaded (via FileRenamePolicy) -- in my application, it uses the epoche datestamp value for the file name */
   File f = multiReq.getFile( nameOfFile );
   FileReader fr = new FileReader( f );
   BufferedReader fileUpload = new BufferedReader( fr );
   while ( (line = fileUpload.readLine()) != null )
   {
      int beginInd = line.indexOf("<title>");
      int endInd = line.indexOf("</title>");
      if (beginInd != -1 && endInd != -1)
      {
         title = line.substring(beginInd, endInd);
      }
   }
   if (title.length() == 0)
   {

/* In my application, the check to see IF it is a valid HTML file is done by seeing if it has a title tag that can be put into the database.  If it doesn't have a title, this method returns false */
      f.delete();
      return false;
   }
   fileUpload.close();
   fr.close();
   addTitleToDB( title );
   return true;

Thanks again tutran.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now