使用jsp将图像从html上传到mysql数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jsp将图像从html上传到mysql数据库相关的知识,希望对你有一定的参考价值。
我想使用jsp在mysql中存储一个图像URL,但面对一个StringIndexOutOfBoundsException plz帮助
我的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("
"));
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("
", pos) + 1;
pos = file.indexOf("
", pos) + 1;
pos = file.indexOf("
", 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我在这里做错了什么?请帮助!!!谢谢!!
答案
我的钱将在这条线上:
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
因为当你这样做时:
int boundaryLocation = file.indexOf(boundary, pos) - 4;
对于boundaryLocation
,你可能很容易得到一个负数。
话虽如此,程序中还有一些其他位置,我可以根据输入看到这种情况。我假设这是某种任务,或者你只是在学习,但即便如此,你应该在你的问题的评论中采纳建议,并以一种形式获得可用的堆栈跟踪。
我确定的一件事是它与MySQL无关。
以上是关于使用jsp将图像从html上传到mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何使用jsp和servlet将多个图像上传到文件夹[重复]
尝试使用 servlet + jsp + mysql 将多个文件(带有任何扩展名)上传到 mysql 数据库中