Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jsp将图像从html上传到mysql数据库_Java_Mysql_Jsp_Upload - Fatal编程技术网

Java 使用jsp将图像从html上传到mysql数据库

Java 使用jsp将图像从html上传到mysql数据库,java,mysql,jsp,upload,Java,Mysql,Jsp,Upload,我打算使用jsp在mysql中存储一个图像url,但面临StringIndexOutOfBoundsException plz帮助 我的html代码: <html> <head> <title>Image upload</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-sca

我打算使用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("\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();
      }
       }
    %>

= 0))
{
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead
0)
{
out.println(“ssjhdsj”);
sendRedirect(“imgsucces.html”);
}
其他的
{
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}
%>
错误:

例外情况: org.apache.jasper.jaspereException:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1

我使用的数据库是mysql 我在这里做错了什么??
请帮忙!!!谢谢!!

我的钱会在这条线上:

int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
因为当你这么做的时候:

int boundaryLocation = file.indexOf(boundary, pos) - 4;
您很容易就得到了
boundaryLocation
的负数

话虽如此,在程序中还有一些其他位置,我可以看到这种情况的发生,这取决于输入。我假设这是某种作业,或者你只是在学习,但即使如此,你也应该在对你的问题的评论中采纳建议,并以一种可以获得可用堆栈跟踪的形式获得它


有一件事我可以肯定,那就是它与MySQL无关。

现在,如果您使用servlets编写这篇文章,您会有一个很好的可用stacktrace-JSP用于表示,为什么要使用
pos=file.indexOf(“filename=\”);pos=file.indexOf(“\n”,pos)+1;pos=file.indexOf(“\n”,pos)+1;pos=file.indexOf(“\n”,pos)+1?我完全不知道“pos=file.indexOf()”这件事。。我在这个网站上找到了他的代码!!。。。这段代码是工作文图像是单独上传的,但不能与其他输入参数一起使用!是的,这是我的一个项目…谢谢你的帮助..那么我需要做什么呢?我已经很久没有使用jsp了,但我建议这样做是为了学习如何调试它,即使servlet也可以…我只是在寻找一个代码,将图像上传到文件夹中,然后将图像的url存储在数据库中!!