gpt4 book ai didi

java - 从输入区域返回空值

转载 作者:行者123 更新时间:2023-11-29 07:25:59 26 4
gpt4 key购买 nike

我试图从 JSP 中的表单标记调用 servlet,但我输入区域的值返回 null,所以我收到 java.lang.NullPointerException 的服务器错误,这很奇怪,因为我在点击之前填充了数据提交按钮

这是表格:

<form action="FileUploadServlet" enctype="multipart/form-data" method="post">

<div class = "col-md-6 col-md-offset-4" id = "articleSection">
<div class = "row" id = "title">
<input type ="text" name = "title" rows = "2" cols = "50" placeholder = "Name of your Article..." id = "artText">
<input type="hidden" name="id" value = '<%=(Integer)session.getAttribute("id")%>'>
</div>

<div id = "image">
<input type="file" name="image" id="fileToUpload">
</div>
<div class = "col-md-2" id = "submit">
<button type="submit" id = "submitBtn" name = "submit" value="articleSubmit">Submit</button>
</div>
</div>

</form>

这是 servlet:

public class FileUploadServlet extends HttpServlet {

public static final String UPLOAD_DIR = "uploads";
public String dbFileName = "";
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

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

int id = Integer.parseInt(request.getParameter("id"));

String title = request.getParameter("title");

Part part = request.getPart("image");//
String fileName = extractFileName(part);//file name

String applicationPath = getServletContext().getRealPath("");

String uploadPath = applicationPath + File.separator + UPLOAD_DIR;
System.out.println("applicationPath:" + applicationPath);
File fileUploadDirectory = new File(uploadPath);
if (!fileUploadDirectory.exists()) {
fileUploadDirectory.mkdirs();
}
String savePath = uploadPath + File.separator + fileName;
System.out.println("savePath: " + savePath);
String sRootPath = new File(savePath).getAbsolutePath();
System.out.println("sRootPath: " + sRootPath);
part.write(savePath + File.separator);
File fileSaveDir1 = new File(savePath);

dbFileName = UPLOAD_DIR + File.separator + fileName;
part.write(savePath + File.separator);

try {
Connection con = DatabaseConnection.getCon();
PreparedStatement pst = con.prepareStatement("insert into articles(title, date, user_id, image) values(?,?,?,?)");
pst.setString(1, title);
pst.setDate(2, sqlDate);
pst.setInt(3, id);
pst.setString(4, dbFileName);

pst.executeUpdate();

response.sendRedirect("articleDetails.jsp?name"+title);
} catch (Exception e) {
out.println(e);
}

}

private String extractFileName(Part part) {//This method will print the file name.
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return "";
}
}

如果我将表单方法更改为“GET”那么它不会返回 null 但我想发布以便我可以将文件插入数据库

编辑:

我能够通过将方法移动到 enctype 之前来修复 nullPointerException,所以它看起来像:

<form action="FileUploadServlet" method="POST" enctype="multipart/form-data">
</form>

但现在我收到了这个错误:

java.io.FileNotFoundException:C:\Users\ttcat\Documents\glassfish5\glassfish\domains\domain1\generated\jsp\JspIpProject\C:\Users\ttcat\Documents\NetBeansProjects\JspIpProject\build\web\uploads\amihan.jpg(文件名、目录名或卷标语法不正确)

如何删除此路径?C:\Users\ttcat\Documents\glassfish5\glassfish\domains\domain1\generated\jsp\JspIpProject\

最佳答案

尝试在你的服务中使用下面的注释

@WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})
@MultipartConfig(maxFileSize = 100 * 1024 * 1024) // 100MB max
public class FileUploadServlet extends FileUploadServlet {

关于java - 从输入区域返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com