?
Solved

Uploading and Verifying an HTML page using a Servlet.

Posted on 2003-03-03
2
Medium Priority
?
260 Views
Last Modified: 2013-11-24
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
Comment
Question by:ozy123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
tutran earned 300 total points
ID: 8066258
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
 

Author Comment

by:ozy123
ID: 8120653
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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question