图像显示错误:java.lang.StringIndexOutOfBoundsException 开始0,结束-1,长度614998
当我用不同的名称多次复制相同的变量时。它告诉我这个错误。我只能插入一张图片
Fileupload.jsp
<form enctype="multipart/form-data" action="/Kastamandap/Admin/Controller/uploadFile.jsp" method="post">
<H1>choose the file to upload</H1><hr>
<input name="file" type="file">
<input name="file2" type="file">
<input name="file3" type="file">
<input name="file4" type="file">
<input type="submit" class="btn btn-primary" value="upload">
</form>
在uploadFile.jsp
中//对于图像1
<%@ page import="java.sql.*,java.io.*,java.util.zip.*,java.lang.*" %>
<%
String saveFile = "";
File ff = null;
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 boundry = 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(boundry, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
ff = new File("D:/programmming/JAVA/eclipse-workspace/Kastamandap/WebContent/Images/" + saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
//Image 1 is successfully inserted
相同的代码,但它向我显示此错误,请帮我注释一下 错误部分
<%
String saveFile3 = "";
File ff3 = null;
String contentType3 = request.getContentType();
if((contentType3 != null) && contentType3.indexOf("multipart/form-data") >= 0 ) {
DataInputStream in3 = new DataInputStream(request.getInputStream());
int formDataLength3 = request.getContentLength();
byte dataBytes3[] = new byte[formDataLength3];
int byteRead3 = 0;
int totalBytesRead3=0;
while(totalBytesRead3 < formDataLength3) {byteRead3=in3.read(dataBytes3,totalBytesRead3,formDataLength3);
totalBytesRead3 += byteRead3;
}
String file3 = new String(dataBytes3);
saveFile3 = file3.substring(file3.indexOf("filename=\"")+10);
//From here it is showing java.lang.StringIndexOutOfBoundsException
saveFile3 = saveFile3.substring(0, saveFile3.indexOf("\n"));
saveFile3 = saveFile3.substring(saveFile3.lastIndexOf("\\") + 1, saveFile3.indexOf("\""));
int lastIndex3 = contentType3.lastIndexOf("=");
String boundry3 = contentType3.substring(lastIndex3 + 1, contentType3.length());
int pos3;
pos3 = file3.indexOf("filename=\"");
pos3 = file3.indexOf("\n", pos3) + 1;
pos3 = file3.indexOf("\n", pos3) + 1;
pos3 = file3.indexOf("\n", pos3) + 1;
int boundaryLocation3 = file3.indexOf(boundry3, pos3) - 4;
int startPos3 = ((file3.substring(0, pos3)).getBytes()).length;
int endPos3 = ((file3.substring(0, boundaryLocation3)).getBytes()).length;
ff3 = new File("D:/programmming/JAVA/eclipse-workspace/Kastamandap/WebContent/Images/"+saveFile3);
FileOutputStream fileOut3 = new FileOutputStream(ff3);
fileOut3.write(dataBytes3, startPos3, (endPos3 - startPos3));
fileOut3.flush();
fileOut3.close();
}
答案 0 :(得分:0)
如何从jsp上传多个文件?
将相关的jar文件添加到lib或maven依赖项。
index.jsp
<html>
<head></head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="fileName"
multiple="multiple" /> <br> <input type="submit" value="Upload" />
</form>
</body>
</html>
UploadServlet.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UploadServlet() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String path1 = "F:\\Temp\\";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024); /* file limit size of 1GB */
com.oreilly.servlet.multipart.Part _part;
while ((_part = parser.readNextPart()) != null) {
if (_part.isFile()) {
FilePart fPart = (FilePart) _part; // get some info about the file
String name = fPart.getFileName();
if (name != null) {
fPart.writeTo(new File(path1 + File.separator + name));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}