07-06
24
<html:file>上传文件标签
作者:Java伴侣 日期:2007-06-24
利用Struts提供的<html:file>来做上传图片。
Jsp页:
form.java
Action.java
可能是我基础太差了,写validator验证写了半天...另外,资源文件和配置信息略
Jsp页:
复制内容到剪贴板 程序代码
<html:errors/>
<html:form action="upload.do" method="POST" enctype="multipart/form-data">
<html:file property="file"/><br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
<html:form action="upload.do" method="POST" enctype="multipart/form-data">
<html:file property="file"/><br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
form.java
复制内容到剪贴板 程序代码
package upload;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** 先验证是否有文件,再验证文件格式.*/
ActionErrors errors = new ActionErrors();
if (file == null || file.equals("")) {
errors.add("ext", new ActionMessage("ext"));
return errors;
}
String fullname = file.getFileName();
String ext = fullname.substring(fullname.indexOf("."), fullname.length());
if (!(ext.equalsIgnoreCase(".jpg") || ext.equalsIgnoreCase(".gif") ||
ext.equalsIgnoreCase(".png"))) {
System.out.println("封装错误信息");
errors.add("ext", new ActionMessage("ext"));
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** 先验证是否有文件,再验证文件格式.*/
ActionErrors errors = new ActionErrors();
if (file == null || file.equals("")) {
errors.add("ext", new ActionMessage("ext"));
return errors;
}
String fullname = file.getFileName();
String ext = fullname.substring(fullname.indexOf("."), fullname.length());
if (!(ext.equalsIgnoreCase(".jpg") || ext.equalsIgnoreCase(".gif") ||
ext.equalsIgnoreCase(".png"))) {
System.out.println("封装错误信息");
errors.add("ext", new ActionMessage("ext"));
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
Action.java
复制内容到剪贴板 程序代码
package upload;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
FileNotFoundException, IOException {
String dir = servlet.getServletContext().getRealPath(""); //根目录
FileForm fileForm = (FileForm) form;
FormFile file = fileForm.getFile();
String fname = file.getFileName();
InputStream streamIn = file.getInputStream(); //输入流
OutputStream streamOut = new FileOutputStream(dir + "/" + fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamIn.close();
streamOut.close();
return new ActionForward("/success.jsp");
}
}
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
FileNotFoundException, IOException {
String dir = servlet.getServletContext().getRealPath(""); //根目录
FileForm fileForm = (FileForm) form;
FormFile file = fileForm.getFile();
String fname = file.getFileName();
InputStream streamIn = file.getInputStream(); //输入流
OutputStream streamOut = new FileOutputStream(dir + "/" + fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamIn.close();
streamOut.close();
return new ActionForward("/success.jsp");
}
}
可能是我基础太差了,写validator验证写了半天...另外,资源文件和配置信息略
评论: 0 | 引用: 0 | 查看次数: 1018
发表评论