Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use multipart-form data in struts

Status
Not open for further replies.

jollyplay

Programmer
Dec 16, 2003
90
Hi,

I am using multipart-form data in my jsp. In my Action class I'm using MultipartWrapper class the line

MultipartWrapper multi =(MultipartWrapper)request;

is not working.

ImportExport.jsp
----------------
<html:form action="/importmain" enctype="multipart/form-data" method="POST" >
<tr>
<td>Input file name</td>
<td>
<html:file property="file" onclick='return fileValue()'/>
</td>
</tr>
<tr>
<td>Delete Records</td>

<td><input type="radio" name="delete" value="deleteYes"/>Yes
<input type="radio" checked name="delete" value="deleteNo"/>No
</td>
</tr>
<tr>
<td>Create Backup</td>
<td><input type="radio" name="backup" value="backupYes"/>Yes
<input type="radio" checked name="backup" value="backupNo"/>No
</td>
</tr>
<tr>
<td><html:submit value="Import" /></td>
<td><html:reset value="Reset"/></td>
</tr>
</html:form>


In My Action class

ImportMainAction.java
---------------------

package bean;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.*;
import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.FilePart;
import java.io.*;

/**
* This class is used import data into database in CSV format
*/
public class ImportMainAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
DatabaseConnection dc=new DatabaseConnection();
Statement stmt=null;
try
{
Connection con=dc.getConnection();
stmt=con.createStatement();
String thisLine;
String appendSql;
String sql,sql1,writeSql;;
HttpSession session=request.getSession(true);
String table=session.getAttribute("tableName").toString();

if (request instanceof MultipartWrapper)
{
System.out.println("Inside if");
MultipartWrapper multi =(MultipartWrapper)request;
MultipartWrapper mpw=(MultipartWrapper)request;
System.out.println("After wrapper");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements())
{
String name = (String)files.nextElement();
System.out.println("Name = "+name);
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
FileInputStream fin=new FileInputStream(f);
BufferedReader myInput=new BufferedReader(new InputStreamReader(fin));
StringTokenizer sToken;
while ((thisLine = myInput.readLine()) != null)
{
sToken=new StringTokenizer(thisLine, ",");
appendSql="";
while(sToken.hasMoreTokens())
{
appendSql=appendSql+"'"+sToken.nextToken()+"',";
}

sql1="insert into bbmp3 values("+appendSql+");";

//System.out.println(sql1.substring(0,sql1.lastIndexOf(",")));

writeSql=sql1.substring(0,sql1.lastIndexOf(","));

sql=writeSql.concat(");");
System.out.println(sql);
stmt.execute(sql);
// System.out.println(thisLine);
}

}

}
}
catch(Exception e)
{
e.printStackTrace();
}
return mapping.findForward("import");
}
}

kindly help me.
Thanks in advane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top