实现方法一:利用struts.jar中的上传组件
程序代码
<%@page contentType="text/html;charset=GBK" language="java" %>
6.建立一个UploadAction 类文件
程序代码
package upload;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UploadForm fileForm = (UploadForm) form;
FormFile file = fileForm.getFile();
FileOutputStream fileOutput = new FileOutputStream(request.getRealPath("/")
+ "upload\\" + file.getFileName());
fileOutput.write(file.getFileData());
fileOutput.flush();
fileOutput.close();
file.destroy() ; // destroy temperaty file
System.out.println("upload ok");
return mapping.findForward("successed");
}
}
7.建立一个UploadForm类文件
程序代码
package upload;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class UploadForm extends ActionForm {
private FormFile file; //注意这里应该是FormFile类型的
public void setFile(FormFile file) {
this.file = file;
}
public FormFile getFile() {
return file;
}
public void reset(ActionMapping mapping,
HttpServletRequest req) {
file = null;
}
}
8.配置struts-config.xml文件
程序代码
http://struts.apache.org/dtds/struts-config_1_2.dtd">
name="uploadForm"
input="/upload.jsp"
path="/upload"
scope="request"
type="upload.UploadAction">
本文作者:佚名 来源:http://hi.baidu.com/ecspell/blog/item/ada556e7e006
CIO之家 www.ciozj.com 微信公众号:imciow