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";
    	}
    }
    

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


    注:转载请注明出处!



  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6706198.html
Copyright © 2011-2022 走看看