如何修复此代码以上传多个文件。我上传单个文件时效果很好。
使用库
的index.jsp
<%@page import="java.io.*" %>
<%@page import="jxl.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File upload</title>
</head>
<body>
<h1>Uploading files</h1>
<form name="uploadForm" action= "index.jsp" method="POST" enctype="multipart/form-data">
<input type="file" name="file" value="" multiple="multiple" width="100"/>
<input type="submit" name="Submit" value="submit" multiple="multiple"/>
<%
String saveFile= new String();
//get content type
String contentType=request.getContentType();
// make sure first of all not null
if((contentType!=null)&&(contentType.indexOf("multipart/form-data")>=0))
{
// begin to read a files
// creating DataInputStream
DataInputStream in = new DataInputStream(request.getInputStream());
//get content links of the request
int formDataLength= request.getContentLength();
// equal size
byte dataBytes[]= new byte[formDataLength];
int totalBytesRead=0;
int byteRead;
while(totalBytesRead<formDataLength)
{
byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=byteRead;
}
//convert it to string
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;
// get length of position
int startPos=((file.substring(0,pos)).getBytes()).length;
int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
saveFile="C:/upload/"+saveFile;
//creating a file object
File ff = new File(saveFile);
try{
//creating a FileOutputStream object
FileOutputStream fileOut= new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, endPos-startPos);
fileOut.flush();
fileOut.close();
}
catch(Exception e){
out.print(e);
}
}
%>
</form>
</body>
</html>