从JSP发送文件到调用接口方法的控制器 - 如何从该请求上传文件? [英] Send a file from JSP to controller which calls a method of interface -How do I upload a file from that request?
问题描述
希望你能帮助我解决我的问题..我正努力工作5天!!
/ p>
在分配给我的模块中,我必须将一个文件,标题,类别,由JSP上传的细节上传到controller.do,它存储 execute()
方法中请求和 responce
c $ c> Command.java 界面。
现在当我通过所有的上传表单细节时,一直到名为 ResourceCommand.java的类实现Command
,它调用DAO并在中间停止执行!!!!
来自 Upload.jsp 的部分代码
< form name =myform_up method =postaction =Controller>
< input type =hiddenname =form_actionvalue =resource/>
< input type =hiddenname =actionvalue =insert/>
< table>
< tr>
< td>文件标题< font color =red> *< / font>< / td>< / tr>
< tr>
< td>< input type =textname =name/>< / td>< / tr>
< tr>
< td>类别< font color =red> *< / font>
< select name =cat>
< option value =Javaselected> Java Material< / option>
< option value =Net> .Net Material< / option>
< option value =C> C and C#Material< / option>
< / select>< / td>
< / tr>
< input type =hiddenname =uploadedbyvalue =< jsp:getProperty name =userBeanproperty =user/> />
< tr>
< td>上传档案:
< input type =filename =file1/>< / td>
< / tr>
< tr>
< td>< br />< input type =checkboxname =t_conditionchecked />我已收到此文件的版权< / td>
< / tr>
< TR>
< td ALIGN =CENTER>< br />
< INPUT class =button blueTYPE =submitvalue =上传/> &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
< INPUT class =button redTYPE =resetvalue =Clear/>
< / td>< / TR>
< / TABLE>
< / FORM>
来自 Controller.java 的部分代码
public void init(ServletConfig config)throws ServletException {
super.init();
System.out.println(我在init);
this.commands.put(login,new LoginCommand());
this.commands.put(resource,new ResourceCommand());
$ b protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {$ b $
processCommand(request,response);
$ b $保护无效doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
System.out.println(我在dopost);
processCommand(request,response);
$ b $ private void processCommand(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
String formAction = request.getParameter(form_action);
System.out.println(implementation formAction =+ formAction);
命令command =(Commands)commands.get(formAction);
command.execute(request,response);
$ b ResourceCommand.java
public class ResourceCommand extends HttpServlet implements命令{
private static final long serialVersionUID = 1L;
public void execute(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
$ b $ if(insert.equalsIgnoreCase(request.getParameter(action))){
this.addResource(request,response);
private void addResource(HttpServletRequest请求,
HttpServletResponse响应)throws ServletException,IOException {
ResourceBean ResourceBean = new ResourceBean ();
mapToResourceBeans(request,ResourceBean);
String result = null;
尝试{
result = new ResourceDAO()。uploadResource(request,ResourceBean);
} catch(Exception ex){
System.err.println(Error in insertion new resource);
if(success.equalsIgnoreCase(result)){
// if Block CODE
}
else {
/ / else块代码
}
}
private void mapToResourceBeans(HttpServletRequest请求,
ResourceBean ResourceBean){
ResourceBean.setName(request.getParameter(name)) ;
ResourceBean.setCat(request.getParameter(cat));
ResourceBean.setUploadedby(request.getParameter(uploadedby));
$ b Command.java
public interface命令{
$ b public void execute(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException;
$ b $ / code>
来自 ResourceDAO.java
public class ResourceDAO {
static final String SUCCESS =success;
static final String FAILURE =failure;
static Logger log = Logger.getLogger(com.a);
public String uploadResource(HttpServletRequest request,
ResourceBean newResource)throws FileNotFoundException {
String result = null;
PreparedStatement stmtInsert = null;
//创建数据库连接
连接con = ConnectionDAO.getJDBCConnection();
PreparedStatement pstmt = con
.prepareStatement(INSERT INTO resource(name,cat,uploadedby,file)values(?,?,?,?));
pstmt.setString(1,newResource.getName());
System.out.println(newResource.getName());
pstmt.setString(2,newResource.getCat());
System.out.println(newResource.getCat());
pstmt.setString(3,newResource.getUploadedby());
System.out.println(newResource.getUploadedby());
String file = request.getParameter(file1);
System.out.println(0);
文件f =新文件(文件);
System.out.println(1);
FileInputStream fis = new FileInputStream(f);
System.out.println(2);
pstmt.setBinaryStream(4,fis,(int)f.length());
System.out.println(3);
int rows = pstmt.executeUpdate();
结果= SUCCESS;
if(rows!= 1){result = FAILURE; }
} catch(SQLException ex){
result = FAILURE;
ConnectionDAO.rollbackJDBCConnection(con);
ex.printStackTrace();
} finally {
ConnectionDAO.commitJDBCConnection(con);
ConnectionDAO.closeStatement(stmtInsert);
ConnectionDAO.closeJDBCConnection(con);
}
返回结果;
$ b $ p $控制台显示
实现formAction =资源
插入新资源时出错
123
净
空
0
1
i回到addResource页面
如果我把 enctype =multipart / form-data
然后我会得到这个.. code> java.lang.NullPointerException
com.kbcss.controller.Controller.processCommand(Controller.java:57)
com.kbcss.controller.Controller.doPost(Controller.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
$ b 我完全失去了帮助......
我感谢你们所有的努力给我的代码:p ...
hello every one i'm working on a project.
Hope u can help me in solving my problem.. I'm struggling for 5 DAYS!!
in a module which is assigned to me I have to upload a file, title, category, uploadedby details from a JSP to controller.do which stores the request
and responce
in a method named execute()
which is present in Command.java
Interface.
now when i pass all the Upload form details,, it goes all the way across to a class named ResourceCommand.java implements Command
which calls a DAO and stops execution in middle !!!!
part of code from Upload.jsp
<form name="myform_up" method="post" action = "Controller">
<input type="hidden" name="form_action" value="resource" />
<input type="hidden" name="action" value="insert" />
<table>
<tr>
<td>Document Title <font color="red">*</font></td></tr>
<tr>
<td><input type="text" name="name" /></td></tr>
<tr>
<td>Category <font color="red">*</font>
<select name="cat">
<option value="Java" selected >Java Material</option>
<option value="Net">.Net Material</option>
<option value="C">C and C# Material</option>
</select></td>
</tr>
<input type="hidden" name="uploadedby" value="<jsp:getProperty name="userBean" property="user" />" />
<tr>
<td>Upload A file:
<input type="file" name="file1"/></td>
</tr>
<tr>
<td><br/><input type="checkbox" name="t_condition" checked />I Had received Copyrights for this Document</td>
</tr>
<TR>
<td ALIGN="CENTER"><br/>
<INPUT class="button blue" TYPE="submit" value="Upload" />
<INPUT class="button red" TYPE="reset" value="Clear" />
</td></TR>
</TABLE>
</FORM>
part of code from Controller.java
public void init(ServletConfig config) throws ServletException{
super.init();
System.out.println("i am in init");
this.commands.put("login", new LoginCommand());
this.commands.put("resource", new ResourceCommand());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in doget");
processCommand(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in dopost");
processCommand(request, response);
}
private void processCommand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String formAction = request.getParameter("form_action");
System.out.println("implementing formAction = " + formAction);
Commands command = (Commands) commands.get(formAction);
command.execute(request, response);
}
}
part of code from ResourceCommand.java
public class ResourceCommand extends HttpServlet implements Commands{
private static final long serialVersionUID = 1L;
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if ("insert".equalsIgnoreCase(request.getParameter("action"))) {
this.addResource(request, response);
}
}
private void addResource(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
ResourceBean ResourceBean = new ResourceBean();
mapToResourceBeans(request, ResourceBean);
String result = null;
try {
result = new ResourceDAO().uploadResource(request, ResourceBean);
} catch (Exception ex) {
System.err.println(" Error in inserting new resource");
}
if ("success".equalsIgnoreCase(result)) {
// if Block CODE
}
else {
//Else block CODE
}
}
private void mapToResourceBeans(HttpServletRequest request,
ResourceBean ResourceBean) {
ResourceBean.setName(request.getParameter("name"));
ResourceBean.setCat(request.getParameter("cat"));
ResourceBean.setUploadedby(request.getParameter("uploadedby"));
}
}
part of code from Command.java
public interface Commands {
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
part of code from ResourceDAO.java
public class ResourceDAO {
static final String SUCCESS = "success";
static final String FAILURE = "failure";
static Logger log = Logger.getLogger("com.a");
public String uploadResource(HttpServletRequest request,
ResourceBean newResource) throws FileNotFoundException {
String result = null;
PreparedStatement stmtInsert = null;
// Create a Database Connection
Connection con = ConnectionDAO.getJDBCConnection();
try {
PreparedStatement pstmt = con
.prepareStatement("INSERT INTO resource(name, cat, uploadedby, file) values(?,?,?,?)");
pstmt.setString(1, newResource.getName());
System.out.println(newResource.getName());
pstmt.setString(2, newResource.getCat());
System.out.println(newResource.getCat());
pstmt.setString(3, newResource.getUploadedby());
System.out.println(newResource.getUploadedby());
String file = request.getParameter("file1");
System.out.println("0");
File f = new File(file);
System.out.println("1");
FileInputStream fis = new FileInputStream(f);
System.out.println("2");
pstmt.setBinaryStream(4, fis, (int) f.length());
System.out.println("3");
int rows = pstmt.executeUpdate();
result = SUCCESS;
if (rows != 1) { result = FAILURE; }
} catch (SQLException ex) {
result = FAILURE;
ConnectionDAO.rollbackJDBCConnection(con);
ex.printStackTrace();
} finally {
ConnectionDAO.commitJDBCConnection(con);
ConnectionDAO.closeStatement(stmtInsert);
ConnectionDAO.closeJDBCConnection(con);
}
return result;
}
}
Console Display..
implementing formAction = resource
Error in inserting new resource
123
Net
null
0
1
i am going back to addResource page
If i place enctype="multipart/form-data"
in form TAG.. then i will get this..
java.lang.NullPointerException
com.kbcss.controller.Controller.processCommand(Controller.java:57)
com.kbcss.controller.Controller.doPost(Controller.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
I am completely LOST..Plz HELP...
解决方案 i found out that i should use Serlvet 3.0 to make thing work..
I thank u all for giving some of u'r effort to my code :p ...
这篇关于从JSP发送文件到调用接口方法的控制器 - 如何从该请求上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!