这是我的jsp表单
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<jsp:include page="common_header.jsp" />
<center>
<form action="projects" method="post" enctype="multipart/form-data" name="productForm" id="productForm">
<div class="row grid_12" id="add_service">
<h2>Add Project</h2>
<div class="grid_6">
<label>Project Name</label>
<div class="clear"></div>
<input type="text" name="name" id="name" class="text_box">
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Project Short Description</label>
<div class="clear"></div>
<textarea name="short_description" id="short_description" class="text_area"></textarea>
</div>
<div class="grid_6 last">
<label>Project Long Description</label>
<div class="clear"></div>
<textarea name="long_description" id="long_description" class="text_area"></textarea>
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Image</label>
<div class="clear"></div>
<input type="file" id="file" name="file"/>
</div>
<div class="grid_6 last">
<label>Link</label>
<div class="clear"></div>
<input type="text" name="link" id="link" class="text_box">
</div>
<div class="clear"></div>
<div class="grid_6">
<label>Status</label>
<div class="clear"></div>
<select id="status" name="status">
<option value="1">In-Progress</option>
<option value="2">Completed</option>
<option value="3">Re-Opened</option>
<option value="4">Just Discussed</option>
</select>
</div>
<div class="grid_6 last">
<label>Client</label>
<div class="clear"></div>
<input type="text" name="client" id="client" class="text_box">
</div>
<div class="clear"></div>
<input type="submit" name="Submit" value="Submit">
</div>
</form>
</center>
<jsp:include page="/common_footer.jsp" />
我将from提交给projects
命名的servlet。
代码如下:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import admin.project_info;
public class projects extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
{
String saveFile = "";
String contentType = req.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(req.getInputStream());
int formDataLength = req.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType
.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
FileInputStream fis;
File f = new File(saveFile);
fis = new FileInputStream(f);
String name = req.getParameter("name");
String short_description = req.getParameter("short_description");
String long_description = req.getParameter("long_description");
String link = req.getParameter("link");
String status = req.getParameter("status");
int astatus = Integer.parseInt(status);
String client = req.getParameter("client");
project_info p1 = new project_info(name, short_description, long_description, link, astatus, (InputStream) fis, (int) (f.length()), client);
p1.create();
}
}
}
我的project_info
代码如下
包管理员;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class project_info
{
String name, short_description, long_description, link, client;
int id, status, file_length;
InputStream image;
public project_info()
{
name = null;
short_description = null;
long_description = null;
link = null;
id = 0;
status = 0;
image = null;
file_length = 0;
}
public project_info(String aname, String sdescription, String ldescription, String alink, int astatus, InputStream apath,int afile_length, String aclient)
{
name = aname;
short_description = sdescription;
long_description = ldescription;
link = alink;
status = astatus;
image = apath;
file_length = afile_length;
client = aclient;
}
//function for create entry in database
public int create()
{
int flag = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/site?user=root&password=root");
String query = "INSERT INTO projects(name, short_description, long_description, status, link, image, client) values(?,?,?,?,?,?,?)";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, name);
statement.setString(2, short_description);
statement.setString(3, long_description);
statement.setInt(4, status);
statement.setString(5, link);
statement.setBinaryStream(6,image, file_length);
statement.setString(7, client);
statement.executeUpdate();
statement.close();
con.close();
flag = 1;
}
catch (Exception e)
{
e.printStackTrace();
flag = 0;
}
return flag;
}
}
过去3天我得到同样的错误..!
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
projects.doPost(projects.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
Apache Tomcat/6.0.20
我没有得到我误解的地方......!
Full StackTrace如下
Dec 30, 2011 11:13:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Admin Website' did not find a matching property.
Dec 30, 2011 11:13:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Temp' did not find a matching property.
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.6.0_10\bin;
Dec 30, 2011 11:13:37 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Dec 30, 2011 11:13:37 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 365 ms
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Dec 30, 2011 11:13:37 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.20
Dec 30, 2011 11:13:38 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Dec 30, 2011 11:13:38 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Dec 30, 2011 11:13:38 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/15 config=null
Dec 30, 2011 11:13:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 296 ms
null
null
Dec 30, 2011 11:13:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet projects threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at projects.doPost(projects.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
您应该完全重写代码的这一部分:
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType
.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
有许多输入会导致此失败。考虑使用简洁的Regex,StringUtils和NumberUtils来简化您的工作。这个解析代码有点味道。
祝你好运。答案 1 :(得分:1)
enctype="multipart/form-data"
是问题..!
简而言之,我们在使用multipart / form-data时无法使用request.getParameter()
:)
答案 2 :(得分:0)
从非整数值创建整数时发生NumberFormatException。 servlet中的以下行导致问题。似乎'status'值是空的。确保在'status'字符串中获得正确的整数值。
int astatus = Integer.parseInt(status);
答案 3 :(得分:0)
我认为问题出在这一行,
int formDataLength = req.getContentLength();
或
int astatus = Integer.parseInt(status);
这里可能是req.getContentLength()为null或者stats为null,当你尝试将null值赋给整数时,会引发numerformat异常。
而不是那样,在分配给任何变量之前检查req.getContentLength()是否为null。
if(req.getContentLength() != null){
int formDataLength = req.getContentLength();
}
if(status != null){
int astatus = Integer.parseInt(status);
}