Java 使用返回空值作为异常的servlet上载文件

Java 使用返回空值作为异常的servlet上载文件,java,servlets,Java,Servlets,您好,我正在使用servlets将图像上载到数据库中。当我提交表格时,我收到一个异常“NULL”,这是我的代码,任何帮助都可以接受。有人能建议我使用哪种方式将文件上传到数据库中,这样数据库上就不会有太多负载,并减少httprequest。先谢谢你 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="requirementlogic

您好,我正在使用
servlets
将图像上载到数据库中。当我提交表格时,我收到一个异常“NULL”,这是我的代码,任何帮助都可以接受。有人能建议我使用哪种方式将文件上传到数据库中,这样数据库上就不会有太多负载,并减少
httprequest
。先谢谢你

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="requirementlogic.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<body>
<form  method="post" action="../InsertImage">
<table>
<tr><TD ><B>Upload Image</B></TD>
<td><input type="file" name="Image" size="20" value=""></TD>
</tr>
<tr>
<td><input type="submit" height="30" width="62"> </td>
</tr>
<tr>

</table>
</form>
</body>
</html> 
这是我的servlet:

package requirementlogic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataBase {
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbname= "deshopa";
    String driver ="com.mysql.jdbc.Driver";
    String Username="root";
    String Password ="";
    public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url+dbname,Username,Password);
        return con;
    }
}
package requirementlogic;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InsertImage
 */
@WebServlet("/InsertImage")
public class InsertImage extends HttpServlet {
    private static final long serialVersionUID = 1L;
       DataBase db;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertImage() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        super.init(config);
        db = new DataBase();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
          PrintWriter pw = response.getWriter();

        try
        {

        String strpath=request.getParameter("image");

        String filepath=strpath.substring(strpath.lastIndexOf(".")+1);
        pw.println(filepath);
        File imgfile = new File(strpath);
        FileInputStream fin = new FileInputStream(imgfile);
        String sql = "insert into upload_image(image,image_name,image_length)";
        PreparedStatement pre = db.getConnection().prepareStatement(sql);

        pre.setBinaryStream(1,fin,(int)imgfile.length());
        pre.setString(2,filepath);
        pre.setLong(3,imgfile.length());
        pre.executeUpdate();
        pre.close();
        pw.println("done");
        }
        catch(Exception e){
            pw.println("direct exception");
        pw.println("Exception is "+ e.getMessage());
        }
    }

}
用于创建表的Sql查询:

CREATE TABLE `upload_image` (
`Image_id` int(11) NOT NULL AUTO_INCREMENT ,
`IMAGE` longblob NULL DEFAULT NULL ,
`Image_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`image_length` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`Image_id`)
)
换成这个

String strpath=request.getParameter("Image"); 
在html表单中,文件字段的名称是
Image
而不是
Image

<td><input type="file" name="Image" size="20" value=""></TD>
有关更多详细信息,请参阅下面的链接

您正在尝试以下操作:

String strpath=request.getParameter("image");
试试这个:

String strpath=request.getParameter("Image");
错误是您正在尝试的名称。名字用大写字母写

万一

File file = new File("yourPath");
file.mkdirs();

你可以试试这个

我试图上传“01_arrival.jpg”它说:例外是01_arrival.jpg(系统找不到指定的文件)@3bu1,如果你不使用
enctype=“multipart/form data”
在您的表单中,您将只获得
文件名
整个图像
,因此请使用此项并按照我的建议进行解析我试图上载“01_arrival.jpg”,它说:异常为01_arrival.jpg(系统无法找到指定的文件)添加异常堆栈跟踪!顺便说一下,您的表单类型不是多部分的。我相信您将遇到这样的问题,然后您将处理一个多部分请求(与简单请求不同)。
File file = new File("yourPath");
file.mkdirs();