铃木一彻演过什么:struts2实现文件上传

来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 02:35:11
index.jsp 上传页面<%@page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>


 
 
 
 
 
 
  ${requestScope.typeError}
 
   enctype="multipart/form-data">
  
  

  
  

  
 

 
 成共上传页面success.jsp<%@ page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>


 
 
 
 
  
  上传成功!
 

  文件标题:
 
 

  文件为:
  " />
 

 
   参看tomcat的web.xml中的mime:

   gif
   image/gif
   在src目录下创建struts.propertiesstruts.locale=zh_CN
struts.i18n.encoding=GBK
struts.multipart.parser=jakarta
struts.xml文件


 
   
   value="globalMessages" />
 
 
 
  
    image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg
  
   /upload
   /success.jsp
   /index.jsp
 

 

 action方法package lee;import java.io.*;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {
 private String title;
 private File upload;
 private String uploadContentType;
 private String uploadFileName;
 private String allowTypes;
 // 接受依赖注入的属性
 private String savePath; // 接受依赖注入的方法
 public void setSavePath(String value) {
  this.savePath = value;
 } private String getSavePath() throws Exception {
  return ServletActionContext.getServletContext().getRealPath(savePath);
 } public void setTitle(String title) {
  this.title = title;
 } public void setUpload(File upload) {
  this.upload = upload;
 } public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 } public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 } public String getTitle() {
  return (this.title);
 } public File getUpload() {
  return (this.upload);
 } public String getUploadContentType() {
  return (this.uploadContentType);
 } public String getUploadFileName() {
  return (this.uploadFileName);
 } @Override
 public String execute() throws Exception {
  System.out.println("开始上传单个文件---");
  System.out.println(getSavePath());
  System.out.println("==========" + getUploadFileName());
  System.out.println("==========" + getUploadContentType());
  System.out.println("==========" + getUpload());
  // 判断是否允许上传
  String filterResult = filterType(this.getAllowTypes().split(","));
//  if (filterResult != null) {
//   ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
//   return filterResult;
//  }
  // 以服务器的文件保存地址和原文件名建立上传文件输出流
  FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
    + getUploadFileName());
  FileInputStream fis = new FileInputStream(getUpload());
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = fis.read(buffer)) > 0) {
   fos.write(buffer, 0, len);
  }
  return SUCCESS;
 } public String filterType(String[] types) {
  String fileType = this.getUploadContentType();
  for (String type : types) {
   if (type.equals(fileType)) {
    return null;
   }
  }
  return INPUT;
 } public String getAllowTypes() {
  return allowTypes;
 } public void setAllowTypes(String allowTypes) {
  this.allowTypes = allowTypes;
 }
}
 xml配置文件
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  struts2
  org.apache.struts2.dispatcher.FilterDispatcher
 

 
  struts2
  /*
 

 
  struts-cleanup
  org.apache.struts2.dispatcher.ActionContextCleanUp
 

 
 struts-cleanup
 /*
 

 
 
  index.jsp
 


 程序可运行