troubleshooting Question

Need help passing text value in multi/part

Avatar of domoaarongato
domoaarongato asked on
Java
24 Comments1 Solution459 ViewsLast Modified:
this question, again.

Hi guys . i have a multipart form that works great.  the only problem i am having is passing the value of a text feild to it since getParamater does not work with cos.   I have to pass an ID to the servlet (so it knows which row to update).   Can anyone give me a hand?  the only thing i can get to work is getName() - which doesnt do what i need at all.   getValues() throws an error when i javac as does getStringValues().

the area in question is this
[code]
                //else if (_part.isParam()) {
                   //this only gets tha NAME of the form field, not the value.   can i specify that i want to know the input value with the name ID?
                     id = _part.getName();

      out.println(id);
                        //}  
[/code]

here is my entire servlet
[code]
package savedata;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.ParamPart;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class imageUpdate extends HttpServlet {
   
    private String fileSavePath;

  public void init(){
      // save uploaded files to a 'data' directory in the web app
      fileSavePath =   getServletContext().getRealPath("/") + "data";
  }
  public void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    java.io.IOException {
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
      HttpSession session = request.getSession(true);
    out.println("<html>");
    out.println("<head>");
    out.println("<title>File uploads</title>");  
    out.println("</head>");
    out.println("<body>");
    out.println("<h2>Here is information about any uploaded files</h2>");
      String id = "";
      String session_ID = session.getId();
      out.println(session_ID);
    try{
        // file limit size of five megabytes
        MultipartParser parser = new MultipartParser(
           request,5 * 1024 * 1024);
        Part _part = null;

            String[] img = new String[10];
            for (int j = 0; j < img.length; j++) {
                  img[j] = "";
            }
            int imgcount = 0;
            


            while ((_part = parser.readNextPart()) != null) {
                if (_part.isFile()) {
                        
                              
               
                    // get some info about the file
                    FilePart fPart = (FilePart) _part;
                    String name = fPart.getFileName();
            
                     
                    if (name != null) {
                        long fileSize = fPart.writeTo(new java.io.File(fileSavePath));
                        out.println("The user's file path for the file: " +
                        fPart.getFilePath() + "<br>");
 
                        img[imgcount] = fPart.getFilePath();

                        out.println("The content type of the file: " +
                        fPart.getContentType()+ "<br>");
                        out.println("The file size: " +fileSize+ " bytes<br><br>");
                        //commence with another file, if there is one
                        imgcount++;
                    }
                    else {
                        out.println("The user did not upload a file for this part.");
                    }
                }    
                //else if (_part.isParam()) {
                     id = _part.getName();
                               out.println(id);
                        //}                
                        
                                                 
            }

            // outside of while
            insert(img,id);          out.println("</body>");
        out.println("</html>");
        out.close();

            
    } catch (java.io.IOException ioe){
       //an error-page in the deployment descriptor is
       //mapped to the java.io.IOException
        throw new java.io.IOException(
            "IOException occurred in: " + getClass().getName());
}
    }
    public void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
        java.io.IOException {
       
        throw new ServletException(
            "GET method used with " + getClass().getName()+
                 ": POST method required.");
    }
public void insert(String[] img,String id) {
    Connection con = null;
     PreparedStatement prep = null;
     String sql = null;
     ResultSet result;
     

    try {
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx?user=root&password=");

      if(!con.isClosed())
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");
           
                       try
           {
         sql = "UPDATE form_main SET img1=?, img2=?, img3=?, img4=?, img5=?, img6=?, img7=?, img8=?, img9=?, img10=? WHERE session_id = '"+id+"'";          
          prep = con.prepareStatement(sql);


    //      System.out.println("img.length = " + img.length);
         for (int i = 0; i < img.length; i++) {
               prep.setString((i + 1), img[i]);
          }
              prep.executeUpdate();
                                  System.out.print("HERE IS THE ID:"+id);

          }
          catch(Exception m)
          {
          System.out.print(m.getMessage());
          }

    } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    } finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
    }
  }
}


[/code]

Thanks in advance.
ASKER CERTIFIED SOLUTION
suprapto45

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 24 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 24 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros