strut2 单文件上传

==========================================jsp上传页面  index.jsp================================

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
     <s:form theme="simple" method="post" action="fileUpload" enctype="multipart/form-data">
      <table width="40%" border="1">
       <tr>
        <td>
         your username:
        </td>
        <td>
         <s:textfield name="userName"/>
        </td>
       </tr>
       <tr>
        <td>
         your password:
        </td>
        <td>
         <s:textfield name="password"/>
        </td>
       </tr>
       <tr>
        <td>
         your file:
        </td>
        <td>
         <s:file name="upload"/>
        </td>
       </tr>
       <tr>
        <td>
         <s:submit value="上传"></s:submit>
        </td>
        <td>
         <s:reset value="取消"></s:reset>
        </td>
       </tr>
      </table>
     </s:form>
  </body>
</html>

 

==============================成功页面 success.jsp==========================================

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
  
  <body>
       username:<s:property value="userName"/><br/>
       password:<s:property value="password"/><br/>
       file Name:<s:property value="uploadFileName"/>
      
  </body>
</html>
===================================Action=======================================

package org.demo.upload.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

 private String userName;
 private String password;
 private File upload;
 private String uploadFileName;
 private String uploadContentType;
 //文件上传的地址
 private String savePath;
 //允许上传的文件类型---当在配置文件中配置了fileUpload过滤器的时候下面的手动就不用写了
// private String allowTypes;
// 
// public String getAllowTypes() {
//  return allowTypes;
// }
//
// public void setAllowTypes(String allowTypes) {
//  this.allowTypes = allowTypes;
// }
  //返回地址
 public String getSavePath() {
  return ServletActionContext.getRequest().getRealPath(this.savePath);
 }

 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public File getUpload() {
  return upload;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public String getUploadFileName() {
  return uploadFileName;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getUploadContentType() {
  return uploadContentType;
 }

 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 @Override
 public String execute() throws Exception {
  //---当在配置文件中配置了fileUpload过滤器的时候下面的手动就不用写了
  //开始过滤文件
//  String filterResult=filterType(allowTypes.split(","));
//  if(filterResult!=null){
//   ActionContext.getContext().put("typeError", "上传失败,文件上传的类型不合法");
//   return filterResult;
//  }
  
  // 源文件
  InputStream is = new FileInputStream(upload);
  //获得路径
  //String root = ServletActionContext.getRequest().getRealPath("/uploadFile");
  File file = new File(this.getSavePath());
  if (!file.exists())
   file.mkdirs();
  //上传文件
  //File target=new File(root, this.getUploadFileName());
  OutputStream os = new FileOutputStream(this.getSavePath()+"\\"+this.uploadFileName);
  byte[] bytes = new byte[1024];
  int len = 0;
  
  while ((len = is.read(bytes)) != -1) {
   os.write(bytes, 0, len);
  }
  is.close();
  os.close();

  return SUCCESS;
 }
 
 //过滤文件
 private String filterType(String[] types){
  String fileType=getUploadContentType();
  System.out.println("fileType:"+fileType);
  for (String s : types) {
   if(s.equals(fileType)){
    return null;
   }
  }
  return INPUT;
 }
}

 

 

 

==================================配置文件=====================================

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <!--设置编码   -->
<!--设置编码   -->
 <constant name="struts.i18n.encoding" value="GBK"/>
 <!-- 设置资源 -->
 <constant name="struts.custom.i18n.resources" value="msg"/> 
  
<package name="fileUpload" namespace="/" extends="struts-default">
  <action name="fileUpload" class="org.demo.upload.servlet.UploadAction">
   <!-- 配置文件过滤器 -->
   
   <interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/bmp,image/pjpeg,image/gif,image/x-png,image/jpeg,image/png</param>
   </interceptor-ref>
   <interceptor-ref name="defaultStack"/>
   <!--在这里正对IE解析类型后image/pjpeg, image/x-png 火狐解析的类型:image/jpeg,image/png  -->
   <!--
   <param name="allowTypes">image/bmp,image/pjpeg,image/gif,image/x-png,image/jpeg,image/png</param>
   -->
   <param name="savePath">/uploadFile</param>
   <result name="success">/one_upload/success.jsp</result>
   <result name="input">/one_upload/index.jsp</result>
  </action>
 </package> 
</struts> 

================处理中文友好提示的配置==========================

在配置中需要的

 其中msg_zh_CN.properties 是配置中文的

struts.messages.error.file.too.large=上传文件过大

struts.messages.error.content.type.not.allowed=上传文件不合法

=====================注意这里的中文需要填写转换过后的ascii码需要到jdk中的native2ascii.exe 将其转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值