org.apache.commons.fileupload上传问题

asdex1999 2007-11-01 03:35:38
我用了JSP+javabean来实现多上传文件 下面是javabean
package com.upload;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.*;
import org.apache.commons.fileupload.*;

public class UploadFile {
private String tmpdir;

private String updir;

private HttpServletRequest request;

public HttpServletRequest getRequest() {
return request;
}

public void setRequest(HttpServletRequest request) {
this.request = request;
}

public String getTmpdir() {
return tmpdir;
}

public void setTmpdir(String string) {
tmpdir = string;
}

public String getUpdir() {
return updir;
}

public void setUpdir(String string) {
updir = string;
}

/**
* * Create directory with the name 'path' *
*
* @param path *
* @return
*/
private String MkDir(String path) {
String msg = null;
java.io.File dir;
dir = new java.io.File(path);
if (dir == null) {
msg = "Error:<BR>Can't create empty directory!";
return msg;
}
if (dir.isFile()) {
msg = "Error:<BR>File name <B>" + dir.getAbsolutePath()
+ "</B> already exists!";
return msg;
}
if (!dir.exists()) {
boolean result = dir.mkdirs();
if (result == false) {
msg = "Error:<BR>Create directory <b>" + dir.getAbsolutePath()
+ "</B> failed!";
return msg;
}
return msg;
} else {
}
return msg;
}

private String getCurDateTime() {
SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
return df.format(new Date());
}

/**
* * Upload files *
*
* @return
*/
public String[] uploadFile() {
String msg = "";
String img = null;
String sFinalName = "";
DiskFileUpload fu = new DiskFileUpload();
// maximum size in byte that permitted to upload
fu.setSizeMax(51200);
// maximum size in byte that will be stored in memory
fu.setSizeThreshold(4096);
// the tempory path that the file(s) will be stored
// when greater than getSizeThreshold()
fu.setRepositoryPath(tmpdir);
// begin read information of upload
List fileItems = null;
try {
fileItems = fu.parseRequest(request);
} catch (FileUploadException e) {
System.err.println("Upload File Failed!");
}
// process each file uploaded
Iterator iter = fileItems.iterator();
// root dir to store file uploaded
String uproot = updir;
// ArrayList used to save uploaded file name
ArrayList uploadedFiles = new ArrayList();
String filepath = updir;
String opmsg = MkDir(filepath);
if (opmsg == null) {
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// Ignore the other type of form field(s) except file
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0) {
continue;
}
name = name.replace('\\', '/');
File fullFile = new File(name);
String sExtension = fullFile.getName().substring(
fullFile.getName().lastIndexOf("."),
fullFile.getName().length());
String sNewFileName = getCurDateTime() + sExtension;
// Set the final filename to sNewFileName
sFinalName = sNewFileName;
// Create the file with the generated name
File savedFile = new File(filepath, sNewFileName);
// If the file already exist, assign a new name to it
for (int k = 0; savedFile.exists(); k++) {
String sTmpFileName = sNewFileName;
String sFileName = sNewFileName.substring(0,sNewFileName.lastIndexOf("."));
savedFile = new File(filepath, sFileName + k
+ sExtension);
sFinalName = sFileName + k + sExtension;
}
try {
item.write(savedFile);
} catch (Exception e1) {
System.err.println("Upload File Failed!");
}
uploadedFiles.add(sFinalName);
}
}
}
String sUploadedFileNames[] = new String[uploadedFiles.size()];
uploadedFiles.toArray(sUploadedFileNames);
return sUploadedFileNames;
}
}
上传JSP upload.jsp
<html>
<head>
<title>Upload</title>
</head>
<body>
<center>
<h1>Upload File Test</h1>
<form name="uploadform" method="POST" action="fileupload.jsp" enctype="multipart/form-data">
File 1:<input name="file1" size="40" type="file"><br />
File 2:<input name="file2" size="40" type="file"><br />
File 3:<input name="file3" size="40" type="file"><br />
<input name="upload" type="submit" value="Upload" />
</form>
</center>
</body>
</html>
fileupload.jsp
 <%@ page pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<jsp:useBean id="up" class="com.upload.UploadFile" scope="page"/>
<% String contextPath = getServletContext().getRealPath("/");
String uploadTmpPath = (contextPath + "tmp").replace('\\', '/');
String uploadRootPath = (contextPath + "upload").replace('\\', '/');
up.setTmpdir(uploadTmpPath);
up.setUpdir(uploadRootPath);
up.setRequest(request);
String[] sUploadFileNames = up.uploadFile();
for (int i = 0; i < sUploadFileNames.length ; i++ ) {
out.println(sUploadFileNames[i]);
}

%>
但是一提交就报错,修改几次都不行,求高手帮下忙
org.apache.jasper.JasperException: An exception occurred processing JSP page /fileupload.jsp at line 10

7: up.setTmpdir(uploadTmpPath);
8: up.setUpdir(uploadRootPath);
9: up.setRequest(request);
10: String[] sUploadFileNames = up.uploadFile();
11: for (int i = 0; i < sUploadFileNames.length ; i++ ) {
12: out.println(sUploadFileNames[i]);
13: }


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:426)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.jxva.framework.filter.ChangeReqEncoding.doFilter(ChangeReqEncoding.java:42)

分不够可以再加
...全文
263 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
asdex1999 2007-11-02
  • 打赏
  • 举报
回复
但是我javabean里面定义的是public String[] uploadFile() 啊
jianghao08 2007-11-01
  • 打赏
  • 举报
回复
String[] sUploadFileNames = up.uploadFile(); up.uploadFile()得到的不是String数组,类型不匹配。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧