你好我正在一個項目上工作。從JSP發送文件到調用接口方法的控制器 - 如何從該請求上傳文件?
希望你能幫助我解決我的問題..我掙扎了5天!
一個模塊中分配給我,我有上傳文件,標題,類別,uploadedby從JSP細節controller.do存儲的request
和responce
在一個名爲方法,它存在於Command.java
接口。
現在,當我通過所有的上傳表單細節時,它會一直到名爲ResourceCommand.java implements Command
的類,它會調用DAO並在中間停止執行!的代碼
部分從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>
從的代碼部分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);
}
}
從的代碼部分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"));
}
}
從
Command.java的代碼
部分
public interface Commands {
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
從的代碼部分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;
}
}
控制檯顯示..
implementing formAction = resource
Error in inserting new resource
123
Net
null
0
1
i am going back to addResource page
如果我放置在enctype="multipart/form-data"
形式TAG ..然後我會得到這..
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)
我完全LOST..Plz幫助...
你讀過這個Q/A :http://stackoverflow.com/q/2422468/1065197? –
我有...但問題包括將請求從課程傳遞到課程,然後傳遞到接口...順便說一下,只有一個Servlet(Controller.java)就是這樣。其餘的只是類。 –
如果您使用的是Serlvet 2.5或之前的版本,那麼在處理'