structs利用 FileUtils 类文件上传示例


这里写图片描述


运用 FileUtils 类

利用 apache 的 IO 流的 FileUtils 类实现操作

在structs的jar包中,有 commons-io-2.2.jar
参考:http://snkcxy.iteye.com/blog/1845862 有更多文件读写,删除操作

文件copy操作

    //结果是cxyapi和cxyapi1在同一目录  
    FileUtils.copyDirectory(new File("D:/cxyapi"), new File("D:/cxyapi1"));   
    //结果是将cxyapi拷贝到cxyapi2下  
    FileUtils.copyDirectoryToDirectory(new File("D:/cxyapi"), new File("D:/cxyapi2"));  

    //拷贝文件  
    FileUtils.copyFile(new File("d:/cxyapi.xml"), new File("d:/cxyapi.xml.bak"));  
    //拷贝文件到目录中  
    FileUtils.copyFileToDirectory(new File("d:/cxyapi.xml"), new File("d:/cxyapi"));  
    //拷贝url到文件  
    FileUtils.copyURLToFile(new URL("http://www.cxyapi.com/rss/cxyapi.xml"), new File("d:/cxyapi.xml"));  

把D:/TestCopy/test2.jpg 文件拷贝一份到目录下的 test3.jpg

try {
    FileUtils.copyFile(new File("D:/TestCopy/test2.jpg"), new File("D:/TestCopy/test3.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

structs文件单文件上传示例

这里写图片描述

structs.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <package name="default" namespace="/" extends="struts-default">

    <action name="uploadaction" class="action.UploadAction" method="upload">
        <result>/index.jsp</result>
    </action>

    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>structs2upload</display-name>
  <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>
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

UploadAction.java

//package action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{

    private File uploadImage;//得到上传的文件
    private String uploadContentType;//得到文件的类型
    private String uploadImageFileName;//得到文件的名称
        @Override
        public String execute() throws Exception {

            return super.execute();
        }
        public String upload(){
            String realpath = ServletActionContext.getServletContext().getRealPath("/images");
            System.out.println("文件存放路径"+realpath);//自己也可以定义
            File file = new File(realpath);
            if(!file.exists()) 
                file.mkdir();//创建目录
            try {
                FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return SUCCESS;
        }

        public File getUploadImage() {
            return uploadImage;
        }
        public void setUploadImage(File uploadImage) {
            this.uploadImage = uploadImage;
        }
        public String getUploadContentType() {
            return uploadContentType;
        }
        public void setUploadContentType(String uploadContentType) {
            this.uploadContentType = uploadContentType;
        }
        public String getUploadImageFileName() {
            return uploadImageFileName;
        }
        public void setUploadImageFileName(String uploadImageFileName) {
            this.uploadImageFileName = uploadImageFileName;
        }
}

upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="uploadaction" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadImage"/>
        <br>
        <input type="submit" value="upload"/>
    </form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
上传成功
</body>
</html>

这里写图片描述

文件上传到的目录(workspace目录)

D:\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\structs2upload\images

structs文件多文件上传示例

同时上传两个文件

upload.jsp

..
<body>
    <form action="uploadaction" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadImage"/><br/>
        <input type="file" name="uploadImage"/><br/>
        <br>
        <input type="submit" value="upload"/>
    </form>
</body>
..

UploadAction.java

//package action;

import java.io.File;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{

    private File[] uploadImage;//得到上传的文件
    private String[] uploadContentType;//得到文件的类型
    private String[] uploadImageFileName;//得到文件的名称
        @Override
        public String execute() throws Exception {

            return super.execute();
        }
        public String upload(){
            String realpath = ServletActionContext.getServletContext().getRealPath("/images");
            System.out.println(realpath);
            File file = new File(realpath);
            if(!file.exists()) 
                file.mkdir();//创建目录
            try {
                for(int i=0;i<uploadImage.length;i++){
                    FileUtils.copyFile(uploadImage[i], new File(file,uploadImageFileName[i]));
                }
                //FileUtils.copyFile(uploadImage, new File(file,uploadImageFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return SUCCESS;
        }
        public File[] getUploadImage() {
            return uploadImage;
        }
        public void setUploadImage(File[] uploadImage) {
            this.uploadImage = uploadImage;
        }
        public String[] getUploadContentType() {
            return uploadContentType;
        }
        public void setUploadContentType(String[] uploadContentType) {
            this.uploadContentType = uploadContentType;
        }
        public String[] getUploadImageFileName() {
            return uploadImageFileName;
        }
        public void setUploadImageFileName(String[] uploadImageFileName) {
            this.uploadImageFileName = uploadImageFileName;
        }   
}

web传递文件必须要求文件大小比较小,(2,3M),文件太大,http上传速度慢
如果是大量文件上传通过socket通讯传递
structs中有常量可以来限制文件上传的大小

<!-- 上传文件的10M大小限制-->
   <constant name="struts.multipart.maxSize" value="11943040"/>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值