多文件上传的 问题 String index out of range: -1在线等!
我做了一个多文件的上传,但是测试的是总是提醒我这样的话String index out of range: -1
不知道是什么意思,希望大家帮帮,代码在这!哪个地方需要改进
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
CompanyintroForm companyintroForm = (CompanyintroForm) form;
FormFile file = companyintroForm.getTheFile();
FormFile file2 = companyintroForm.getTheFile2();
FormFile file3 = companyintroForm.getTheFile3();
Map <String, FormFile> fileMap = new HashMap <String, FormFile> ();
fileMap.put( "file " , file);
fileMap.put( "file2 " , file2);
fileMap.put( "file3 " , file3);
Set fileSet = fileMap.entrySet();
Iterator iter = fileSet.iterator();
// 取当前系统路径E:\Tomcat5\webapps\strutsUpload\ 其中strutsUpload为当前context
String filePath = this.getServlet().getServletContext().getRealPath( "/ ");
// 保存文件的文件夹
File savePath = new File(filePath + "UploadFiles\\ ");
filePath = filePath+ "UploadFiles\\ ";
if (!savePath.exists()) {
savePath.mkdir();
}
while (iter.hasNext()) {
Map.Entry unit = (Map.Entry) iter.next();
String key = (String) unit.getKey();
FormFile file0 = (FormFile) unit.getValue();
//文件大小符合要求,且是图片文件
if ((file.getFileSize() > = 1)&& DealPhoto.isPhoto(file)) {
//图片类别
String photoType = key.substring(key.indexOf( "* ") + 1, key
.lastIndexOf( "* "));
//图片描述
String photoName = key.substring(key.lastIndexOf( "* ") + 1, key
.length());
//存数据库操作,在数据库中保存文件的名称,类型,及在服务器上的相对路径
//
//判断是否重名
if(DealPhoto.isFileExist(file.getFileName(),filePath))
DealPhoto.rename(file.getFileName(),filePath);
try {
InputStream stream = file.getInputStream();// 把文件读入
// 建立一个上传文件的输出流
OutputStream bos = new FileOutputStream(filePath+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return mapping.findForward( "haha ");
}
}
[解决办法]
某个值越界了吗,我也遇到过