将文件从JSP发送到控制器,该控制器调用接口方法 - 如何从该请求上传文件?

时间:2013-10-15 16:31:25

标签: java jsp servlets file-upload

你好,我正在为一个项目工作。

希望你能帮助我解决我的问题..我正在努力奋斗5天!!

在分配给我的模块中,我必须上传一个文件,标题,类别,上传详细信息从JSP到controller.do,它将requestresponce存储在一个名为{的方法中{1}} 接口中存在的{1}}。

现在,当我传递所有上传表单详细信息时,它会一直传递给名为execute()的类,该类调用DAO并在中间停止执行!!!!

来自 Upload.jsp

的部分代码
Command.java

来自 Controller.java

的部分代码
ResourceCommand.java implements Command

来自 ResourceCommand.java

的部分代码
<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" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <INPUT class="button red" TYPE="reset" value="Clear" />
            </td></TR>
    </TABLE>
</FORM>

来自 Command.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);            
    }
}

来自 ResourceDAO.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"));             
    }   
}

控制台显示..

public interface Commands {

    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

}

如果我将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; } } 放入TAG格式..那么我会得到这个......

implementing formAction = resource
 Error in inserting new resource
123
Net
null
0
1
i am going back to addResource page

我完全失去了......帮助......

2 个答案:

答案 0 :(得分:1)

这条线是56吗?错误之前的那个?

Commands command = (Commands) commands.get(formAction);    

如果在null地图(您未显示)中找不到该命令,则可能会返回commands。您至少应该检查从地图返回的有效命令。

如果找不到匹配的内容,Map将返回null。在下一行,您使用它并获得NullPointerException

答案 1 :(得分:0)

我发现我应该使用Servlet 3.0来使事情发生.. 我感谢大家为我的代码付出了一些努力:p ...