IT虾米网

Struts文件上传详解

lxf 2019年09月30日 编程语言 127 0

用两个包:一个是commons-fileupload-1.3.2.jar,另一个是commons-io-2.2.jar

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="Shangchuan_add" method="post" enctype="multipart/form-data"> 
<input type="file" name="file"><br><br> 
<input type="submit" value="submit" > 
</form> 
 
</body> 
</html>

做Action。
三个成员:1.File file上传的文件 2.String fileFileName属性名 3.String fileContentType属性名
org.apache.commons.io.FileUtils.copyFile很简单。

 

sturts.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> 
  
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
    <constant name="struts.devMode" value="true" /> 
    <constant name="struts.i18n.encoding" value="UTF-8"></constant> 
    <constant name="struts.ui.theme" value="simple"></constant> 
    <constant name="struts.multipart.maxSize" value="20971520"></constant><!-- 修改限制上传文件大小,默认是2097152 --> 
 
    <package name="default" namespace="/" extends="struts-default"> 
 
         <action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}"> 
            <result> 
                {1}_{2}.jsp 
            </result> 
        </action> 
    </package> 
 
    <include file="example.xml"/> 
 
    <!-- Add packages here --> 
 
</struts>

 

解决重命名问题

package com.itnba.maya.controller; 
 
import java.io.File; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import org.apache.commons.io.FileUtils; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class ShangchuanAction extends ActionSupport { 
    private File file; 
    private String fileFileName;//上传文件的名字 
    private String fileContentType;//上传文件的格式 
     
     
    public File getFile() { 
        return file; 
    } 
 
 
 
    public String getFileFileName() { 
        return fileFileName; 
    } 
 
 
 
    public void setFileFileName(String fileFileName) { 
        this.fileFileName = fileFileName; 
    } 
 
 
 
    public String getFileContentType() { 
        return fileContentType; 
    } 
 
 
 
    public void setFileContentType(String fileContentType) { 
        this.fileContentType = fileContentType; 
    } 
 
 
 
    public void setFile(File file) { 
        this.file = file; 
    } 
 
 
 
    public String input(){ 
         
        return SUCCESS; 
    } 
    public String add() throws IOException{ 
        //为了防止上传文件重名,在文件前面加上当前时间 
        long l=System.currentTimeMillis(); 
        Date date=new Date(l); 
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddhhmmssSSS");//精确到毫秒 
        String pre=sdf.format(date); 
        String fileName = pre+fileFileName; 
        File destFile = new File("d:\\"+fileName);//放到哪里的路径+文件名 
        FileUtils.copyFile(file, destFile); 
         
        return SUCCESS; 
    } 
 
}

上传完成

 

 

*************************************************************************************

多个文件同时上传,Action类里面三个成员定义成数组,用数组来接收

package com.itnba.maya.controller; 
 
import java.io.File; 
import java.io.IOException; 
 
import org.apache.commons.io.FileUtils; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class ShangchuanduoAction extends ActionSupport { 
    private File[] file; 
    private String[] fileFileName; 
    private String[] fileContentType; 
    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; 
    } 
    public String[] getFileContentType() { 
        return fileContentType; 
    } 
    public void setFileContentType(String[] fileContentType) { 
        this.fileContentType = fileContentType; 
    } 
    public String input(){ 
         
        return SUCCESS; 
    } 
    public String add() throws IOException{ 
        //遍历数组 
        for(int i=0;i<file.length;i++){ 
            File item= file[i]; //取出每个File 
            if(item!=null){ 
                File temp = new File("d:\\"+fileFileName[i]);//去出每个的fileFileName 
                FileUtils.copyFile(item, temp); 
            } 
        } 
         
        return SUCCESS; 
    } 
 
}

 

jap页面:

<%@ 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> 
<h1>多文件上传</h1> 
<form action="Shangchuanduo_add" method="post" enctype="multipart/form-data"> 
 
附件1:<input type="file" name="file"><br> 
附件2:<input type="file" name="file"><br> 
附件3:<input type="file" name="file"><br> 
附件4:<input type="file" name="file"><br> 
附件5:<input type="file" name="file"><br><br> 
<input type="submit" value="submit" > 
</form> 
 
</body> 
</html>

上传成功

 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

struts转换器详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。