gpt4 book ai didi

java - 处理上传文件时出错

转载 作者:行者123 更新时间:2023-12-03 23:45:08 25 4
gpt4 key购买 nike

我正在尝试读取上传的文件并使用 JSP 将其保存在数据库中。

<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<html>


<%
int val =0;
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
System.out.println("saveFile=" + saveFile);

saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("saveFile" + saveFile);

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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%>


<%
Connection con=null;
PreparedStatement pstatement = null;
String line = null;
String value=null;






try
{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));

while (( line = input.readLine()) != null){
contents.append(line);

}


value = contents.toString();
System.out.println("Value:"+value);

Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:jtds:sqlserver://W2K8SERVER:1433/career","sa","Alpha#123" );

java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;



String queryString = "INSERT INTO file_tbl set file_data='"+value+"',file_date='"+strDateNew+"'";

//out.println(queryString);

pstatement=con.prepareStatement(queryString);

val = pstatement.executeUpdate();

if(val>0)
{
%>
<br><br>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>
<%
}


}
catch(Exception e)
{}
}
%>
</html>

但是,我收到以下错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /sendfile.jsp at line 26
->root cause
java.lang.StringIndexOutOfBoundsException: String index out of range: -53652

这是怎么引起的,我该如何解决?

最佳答案

那里有大量的 String.IndexOf() 调用,当您传递给它的参数在字符串中找不到时,没有任何类型的异常处理,这将返回一个-1 并导致异常,因为您将其嵌套在另一个调用中,例如 Substring,该调用期望参数为非负整数。我猜这就是您的 StringIndexOutOfBoundsException 的来源。

关于java - 处理上传文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5405470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com