我想用jsp在mysql中存储一个图片网址,但面对一个StringIndexOutOfBoundsException plz help
我的HTML代码:
<html>
<head>
<title>Image upload</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form name="form1" id="form1" action="UploadImage.jsp" method="post"
enctype="multipart/form-data">
<!--<input type="hidden" name="hiddenfield1" value="ok">-->
<h1 align="center">Image Upload</h1>
<hr color="pink" width="50%" size="5%" align="center">
<br>
<table border="5" align="center" bordercolor="grey">
<tr>
<td>Name Of The Image</td>
<td><input type="text" name="n1" size="50" placeholder="abc"></td>
</tr>
<tr>
<td>Size</td>
<td><input type="text" name="s1" size="10" placeholder="100mb"></td>
</tr>
<tr>
<td>Type</td>
<td><input type="text" name="t1" size="20" placeholder="eg: pdf" ></td>
</tr>
<tr>
<td>Date</td>
<td><input type="date" name="d1" size="15" ></td>
</tr>
<tr>
<td>File Upload :</td>
<td>
<input type="file" name="file" ></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="INSERT" class="btn">
</td>
</tr>
</table>
</form>
</body>
</html>
我的jsp代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*" %>
<%@ page language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.io.output.*"%>
<%@page import="java.util.Iterator,java.util.List" %>
<%@page import="org.apache.commons.fileupload.*,
org.apache.commons.fileupload.disk.*,
org.apache.commons.fileupload.servlet.*" %>
<%@ page import="java.util.*"%>
<%@page import="javax.servlet.http.*" %>
<%
String name="";
String size="";
String type="";
String date="";
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
if(item.getFieldName().equals("n1"))
{
name=item.getString();
}
if(item.getFieldName().equals("s1"))
{
size=item.getString();
}
if(item.getFieldName().equals("t1"))
{
type=item.getString();
}
if(item.getFieldName().equals("d1"))
{
date=item.getString();
}
}
}
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data")
>= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.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("D:/Upload/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<%
Connection connection = null;
PreparedStatement psmnt = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/s36",
"root", "legend");
psmnt = connection.prepareStatement("insert into
image(name,size,type,DOU,url) values(?,?,?,?,?)");
psmnt.setString(1,name);
psmnt.setString(2,size);
psmnt.setString(3,type);
psmnt.setString(4,date);
psmnt.setString(5,ff.getPath());
int s = psmnt.executeUpdate();
if(s>0)
{
out.println("ssjhdsj");
response.sendRedirect("ImgSuccess.html");
}
else
{
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
%>
错误:
异常: org.apache.jasper.JasperException:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1
我使用的数据库是mysql 我在这做错了什么? 请帮助!!!谢谢你!!