zoukankan      html  css  js  c++  java
  • 【Struts2学习笔记(9)】单文件上传和多文件上传


    (1)单文件上传

    第一步:在WEB-INF/lib下增加commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。

    这两个文件能够从http://commons.apache.org/下载。(假设是直接使用Add Struts capacities 则能够不用这几个jar包)


    第二步:把form表的enctype设置为:“multipart/form-data“,例如以下:

    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
      <input  type="file" name="uploadImage">
    </form>

    第三步:在Action类中加入下面属性:

    package cn.lc.action;
    
    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    
    public class HelloWorldAction {
    	private File image;
    	private String imageFileName;
    
    	public String getImageFileName() {
    		return imageFileName;
    	}
    	public void setImageFileName(String imageFileName) {
    		this.imageFileName = imageFileName;
    	}
    	public File getImage() {
    		return image;
    	}
    	public void setImage(File image) {
    		this.image = image;
    	}
    	public String addUI() {
    		return "success";
    	}
    	public String execute() throws Exception {
    
    		// 获得路径
    		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    		System.out.println(realpath);
    		//推断文件不为空的时候才保存
    		if (image != null) {
    			File savefile = new File(new File(realpath), imageFileName);
    			//获得稳健文件夹 假设不存在则创建
    			if (!savefile.getParentFile().exists()) {
    				savefile.getParentFile().mkdirs();
    			}
    			FileUtils.copyFile(image, savefile);
    			ActionContext.getContext().put("message", "上传成功");
    		}
    		return "success";
    	}
    }
    

    第四步:上边的代码在上擦混大文件的时候会出现错误这是由于默认的大小仅仅有几兆 假设须要上传稍大一些的文件则须要改动struts.xml默认的文件大小:

    <struts>
      <!--   使用常量来设置上传文件的大小  大小为10兆左右-->
        <constant name="struts.multipart.maxSize" value="10701096"/>
     
    	<package name="employee" namespace="/control/employee" extends="struts-default">
    		<action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
    			<result name="success">/WEB-INF/page/message.jsp</result>
    		</action>
    	</package>
    </struts>
    

    (2)多文件上传

    第一步:在WEB-INF/lib下增加commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件能够从http://commons.apache.org/下载。
    第二步:把form表的enctype设置为:“multipart/form-data“,例如以下:

    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
      <input  type="file" name="uploadImages">
      <input  type="file" name="uploadImages">
    </form>


    第三步:在Action类中加入下面属性,属性红色部分相应于表单中文件字段的名称:

    package cn.lc.action;
    
    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    
    public class HelloWorldAction {
    	private File[] image;
    	private String[] imageFileName;
    
    	public File[] getImage() {
    		return image;
    	}
    
    	public void setImage(File[] image) {
    		this.image = image;
    	}
    
    	public String[] getImageFileName() {
    		return imageFileName;
    	}
    
    	public void setImageFileName(String[] imageFileName) {
    		this.imageFileName = imageFileName;
    	}
    
    	public String addUI() {
    		return "success";
    	}
    
    	public String execute() throws Exception {
    
    		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    		System.out.println(realpath);
    		if (image != null) {
    			File savedir = new File(realpath);
    			if (!savedir.exists())
    				savedir.mkdirs();
    			for (int i = 0; i < image.length; i++) {
    				File savefile = new File(savedir, imageFileName[i]);
    				FileUtils.copyFile(image[i], savefile);
    			}
    			ActionContext.getContext().put("message", "上传成功");
    		}
    		return "success";
    	}
    }
    

    相同的 也能够设置文件上传的限制大小!


    注:转载请注明出处!



  • 相关阅读:
    Oracle手工增加排序区避免SQL使用临时表空间排序产生物理IO
    Oracle中"TABLE ACCESS FULL"的”欺骗时刻“
    Oracle关于12C新特性InMemory踩坑历程
    Oracle19C关于参数sec_case_sensitive_logon控制密码大小写敏感问题
    友链
    RESTful API
    不自由的自由职业
    惊了!修仙=编程??
    [Git专题] 环境搭建
    Linux系统僵尸进程详解
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6706198.html
Copyright © 2011-2022 走看看