码迷,mamicode.com
首页 > Web开发 > 详细

struts2 上传文件

时间:2016-05-20 06:10:08      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.multipart.maxSize" value="20971520000"></constant>
  <package name="default" namespace="/" extends="json-default">
    <action name="uploadAction" class="com.huawei.s2.action.UploadAction" />
    <action name="uploadBatchAction" class="com.huawei.s2.action.UploadBatchAction" />
  </package>
</struts>

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>This is my JSP 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">
    <script src="js/jquery-1.8.3.js"></script>
  </head>
  <body>
    <form action="uploadAction!uploadFile" enctype="multipart/form-data" method="post">
      <input name="uname" value="张三" /><br>
      <input type="file" name="file" /><br>
      <input type="submit" value="提交">
    </form>
  </body>
</html>

index2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>This is my JSP 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">
    <script src="js/jquery-1.8.3.js"></script>
  </head>
  <body>
    <form action="uploadBatchAction!uploadFile" enctype="multipart/form-data" method="post">
      <input name="uname" value="张三" /><br>
      <fieldset>
        <legend><input type="button" value="添加"/></legend>
        <div><input type="file" name="files" /><input type="button" value="删除"></div>
        <div><input type="file" name="files" /><input type="button" value="删除"></div>
        <div><input type="file" name="files" /><input type="button" value="删除"></div>
      </fieldset>
      <input type="submit" value="提交">
    </form>
  </body>
</html>

UploadAction:

package com.huawei.s2.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class UploadAction {
  private String uname;
  private File file;// File 类型属性接收上传文件 属性名必须匹配表单域file的名字
  private String fileFileName;// 要想获取上传文件的文件名 ,属性名必须这样写: file表单域的name值+FileName
  public void uploadFile() throws Exception{
    System.out.println(uname+"========="+fileFileName);
    String realPath =ServletActionContext.getServletContext().getRealPath("/resources");
    FileUtils.copyFile(file, new File(realPath+"/"+UUID.randomUUID().toString()+"_"+fileFileName));
    /*BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(realPath+"/"+fileFileName));
    int temp = -1;
    while((temp=bis.read())!=-1){
      bos.write(temp);
    }
    bos.flush();
    bos.close();
    bis.close();*/
  }
  public String getUname() {
    return uname;
  }
  public void setUname(String uname) {
    this.uname = uname;
  }
  public File getFile() {
    return file;
  }
  public void setFile(File file) {
    this.file = file;
  }
  public String getFileFileName() {
    return fileFileName;
  }
  public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
  }
}

UploadBatchAction:

package com.huawei.s2.action;

import java.io.File;
import java.util.Arrays;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class UploadBatchAction {
  private String uname;
  private File[] files;// File 类型属性接收上传文件 属性名必须匹配表单域file的名字
  private String[] filesFileName;// 要想获取上传文件的文件名 ,属性名必须这样写: file表单域的name值+FileName
  public void uploadFile() throws Exception{
    System.out.println(uname+"========="+Arrays.toString(filesFileName));
    String realPath =ServletActionContext.getServletContext().getRealPath("/resources");
    for(int i=0;i<files.length;i++){
      FileUtils.copyFile(files[i], new File(realPath+"/"+UUID.randomUUID().toString()+"_"+filesFileName[i]));
    }
  }
  public String getUname() {
    return uname;
  }
  public void setUname(String uname) {
    this.uname = uname;
  }
  public File[] getFiles() {
    return files;
  }
  public void setFiles(File[] files) {
    this.files = files;
  }
  public String[] getFilesFileName() {
    return filesFileName;
  }
  public void setFilesFileName(String[] filesFileName) {
    this.filesFileName = filesFileName;
  }
}

struts2 上传文件

标签:

原文地址:http://www.cnblogs.com/hwgok/p/5510735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!