今天我用commons-fileupload組件上傳文件,代碼是正確的,而且我把commons-fileupload-1.2.1.jar,commons-io-1.4.jar也導(dǎo)入到項(xiàng)目目項(xiàng)中,但總是出現(xiàn)這樣那樣的錯(cuò)誤,讓我花了不少的時(shí)間,所以我把相關(guān)發(fā)出來,對(duì)有此類的問題的朋友有所幫助
如:
java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
……………………
在網(wǎng)上查了很多資料,后來總結(jié)了一下,如果你是在struts2項(xiàng)目中用commons-fileupload組件的話,那就應(yīng)
該把修改web.xml,把原來的
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
改為
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
而上傳頁面調(diào)用時(shí)直接用文件名調(diào)用(如upload.jsp)。struts2只處理 *.action 的請(qǐng)求。
或者
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
去掉也行
若是java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException錯(cuò)誤
那就有可能是導(dǎo)包的問題,若用的是右擊項(xiàng)目,選擇屬性,然后導(dǎo)入相應(yīng)的包,就有可能出顯此類的錯(cuò)誤,解決方法是:把commons-fileupload-1.2.1.jar,commons-io-1.4.jar復(fù)制到到WEB-INF下的lib目錄中,雖然效果是一樣,但不會(huì)出錯(cuò)了
附:
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
文件1:<input type="file" name="file1"><br>
文件2:<input type="file" name="file2"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
UploadServlet.java
package com.test.servlet1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload");
// 設(shè)置傳輪輸文件的大小,當(dāng)小于1024時(shí),把文件存在茲盤上
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list){
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString("GB2312");
request.setAttribute(name, value);
}else{
String name = item.getFieldName();
String value = item.getName();
int start = value.lastIndexOf("\\");
String fileName = value.substring(start+1);
request.setAttribute(name, fileName);
//item.write(new File(path,fileName));
OutputStream os = new FileOutputStream(new File(path,fileName));
InputStream is = item.getInputStream();
// 將文件is寫到os里
byte[] buffer = new byte[400];
int lenght = 0 ;
while((lenght = is.read(buffer ))>0){
os.write(buffer,0,lenght);
}
os.close();
is.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
System.out.println("文件上傳出錯(cuò)"+e.getMessage());
e.printStackTrace();
}
request.getRequestDispatcher("upload/result.jsp").forward(request, response);
}
}
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'result.jsp' starting page</title>
</head>
<body>
用戶名:${requestScope.username }
密碼:${requestScope.password }
文件1:${requestScope.file1 }
文件2:${requestScope.file2 }
</body>
</html>