(!(canSuffix.indexOf("."+suffix[i])>=0))) {
description[i] = "ERR: File suffix is wrong.";
return false;
}
else {
return true;
}
}
//上传文件转换
private void transferFile(int i) {
String x = Long.toString(new java.util.Date().getTime());
try {
objectFileName[i] = x + "." + suffix[i];
FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[count] = "ERR: The file " + sourceFile[count] +
" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description[count] = "Right: The file " + sourceFile[count] +
" has been transfered successfully.";
}
out.close();
if (!successful) {
sis.close();
File tmp = new File(objectPath + objectFileName[count]);
tmp.delete();
}
}
catch (IOException ioe) {
description[i] = ioe.toString();
}
}
public static void main(String[] args) {
System.out.println("Test OK");
}
}
文件上传调用:MoqUpload.jsp
〈%@ page contentType="text/html; charset=GB2312" %>
〈html>
〈head>
〈title>文件上载〈/title>
〈/head>
〈body>
〈form action="MoqUploadSubmit.jsp" enctype="MULTIPART/FORM-DATA" method="post">
作者姓名:〈input type="text" name="Author" />
〈br />
公司名称:〈input type="text" name="Company" />
〈br />
文件描述:〈input type="text" name="Comment" />
〈br />
选择文件1:〈input type="file" name="filename1" />
〈br />
选择文件2:〈input type="file" name="filename2" />
〈br />
选择文件3:〈input type="file" name="filename3" />
〈br />
选择文件4:〈input type="file" name="filename4" />
〈br />
〈input type="submit" value="上载" />
〈/form>
〈/body>
〈/html>
文件上传提交:MoqUploadSubmit.jsp
〈%@ page contentType="text/html;charset=gb2312"%>
〈jsp:useBean id="fileBean" scope="page" class="net.moq.www.MoqUploadBean" />
〈%
fileBean.setObjectPath("D:\\Temp\\");
fileBean.setSize(10000*1024);
fileBean.setSuffix(".gif.jpg.png.jpge.html.htm");
fileBean.setSourceFile(request);
String [] saSourceFile = fileBean.getSourceFile();
String [] saObjectFile = fileBean.getObjectFileName();
String [] saDescription = fileBean.getDescription();
int iCount = fileBean.getCount();
String sObjectPath = fileBean.getObjectPath();
for(int i=0;i〈iCount;i++) {
out.println("〈br>源始文件:");
out.println(saSourceFile[i]);
out.println("〈br>目标文件:");
out.println(sObjectPath+saObjectFile[i]);
out.println("〈br>上传说明:");
out.println(saDescription[i]);
out.println("〈br>");
}
out.println("〈br>作者:" + fileBean.getFieldValue("Author"));
out.println("〈br>公司:" + fileBean.getFieldValue("Company"));
out.println("〈br>说明:" + fileBean.getFieldValue("Comment"));
%>
……