F.新建一个类命名为UploadFileClass,实现文件输出的功能。
package com.yourcompany.upload;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;
public class UploadFileClass {
// 方法用于上传文件到远程服务器上
public UploadFileClass(FormFile formFile, String fileName,
String toServerPath) throws Exception {
if (formFile != null && toServerPath != null) {
InputStream inputStream = formFile.getInputStream();
OutputStream outputStream = new FileOutputStream(toServerPath + "/"
+ fileName);
int bytesRead = 0;
byte[] bytesBuf = new byte[formFile.getFileSize()];
while ((bytesRead = inputStream.read(bytesBuf)) != -1) {
outputStream.write(bytesBuf, 0, bytesRead);
}
outputStream.write(bytesBuf);
outputStream.close();
inputStream.close();
} else {
throw new Exception("This error caused by some parameters is null!");
}
}
}
package com.yourcompany.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.yourcompany.struts.form.UploadForm;
import com.yourcompany.upload.UploadFileClass;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm) form;
FormFile formFile = uploadForm.getUploadFile();
try {
String fileName = formFile.getFileName();
System.out.println(fileName);
String toServerPath = request.getRealPath("/");
UploadFileClass uploadFileClass = new UploadFileClass(formFile,
fileName, toServerPath);
return mapping.findForward("success");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("failed");
}
}
}
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private FormFile uploadFile;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
String fileName = uploadFile.getFileName();
if (fileName == null || "".equals(fileName)) {
errors.add("uploadFile", new ActionError("noUserFile"));
}
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
}
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
JSP for UploadForm form
uploadFile :
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
文件上传成功!
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'failed.jsp' starting page
文件上传失败!
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
3
detail
3
0
action
*.do
upload.jsp
================================OVER============================
工程源码请到地址:
http://download.csdn.net/detail/captaingan/4109944
下载