我正在尝试将一些图像文件上传到我的数据库。当我运行我的代码时它产生错误java.lang.IndexOutOfBoundsException:
。但是相同的代码在另一种形式下运行良好。
我的代码是:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String uname = (String) request.getSession().getAttribute("uname");
//set content type
response.setContentType("text/html");
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
if (! ServletFileUpload.isMultipartContent(request)) {
System.out.println("sorry. No file uploaded");
return;
}
// parse request
List<?> items = uploadHandler.parseRequest(request); //getting error
System.out.println(items.size());
// get uploaded file
FileItem file = (FileItem) items.get(0);
Connection con=prepareConnection();
con.setAutoCommit(false);
//Part photo=request.getPart("photo");
BufferedImage originalImage=ImageIO.read(file.getInputStream());
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(resizeImageJpg, "png", baos);
baos.flush();
InputStream is = new ByteArrayInputStream(baos.toByteArray());
StringBuilder sb=new StringBuilder(1024);
sb.append("insert into ").append(uname).append("PROFILEPICTURE values(?,?)");
String query = sb.toString();
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, 1);
ps.setBinaryStream(2, is);
int i=ps.executeUpdate();
con.commit();
con.close();
if(i==1)
{
RequestDispatcher dispatcher = request.getRequestDispatcher("/sp/profile");
if(dispatcher != null) {
dispatcher.forward(request, response);
}
}
else
{
out.println("Error ocuured");
}
out.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
控制台输出:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at skypark.AddProfilePict.doPost(AddProfilePict.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的表格是:
<form id="form" method="post" action="AddProfilePict" enctype="multipart/form-data">
<input type="file" accept="image/*" style="width: 450px; border:0px; background:none; margin-top:5px; width:245px;" value="">
<input class="upld" type="submit" value="Upload">
</form>
任何人都可以帮我弄清楚导致错误的原因吗?
答案 0 :(得分:0)
items
应该是空的。您应首先检查它的长度,然后尝试使用
if(! items.isEmpty()) {
FileItem file = (FileItem) items.get(0);
}
或
if(items.size() > 0) {
FileItem file = (FileItem) items.get(0);
}