zoukankan      html  css  js  c++  java
  • struts2文件上传.

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    
    		<s:form action="uploadFile" method="post" enctype="multipart/form-data"  namespace="/FileUpload">
    			<s:textfield name="username" label="username"></s:textfield>
    			<s:password name="password" label="password" ></s:password>
    			<s:file name="myfile" label="select file"></s:file>
    			<s:submit></s:submit>
    		</s:form>
    
    <!--file:struts.xml-->
    
    	<package name="uploadFileDemo" namespace="/FileUpload"
    		extends="struts-default">
    		<action name="uploadFile" class="test.action.UploadFile">
    			<result>/FileUpload/upload-success.jsp</result>
    			<result name="input">/FileUpload/upload.jsp</result>
    			<interceptor-ref name="fileUpload">
    				<param name="allowedExtensions">
    					.jpg,.txt,.log
    				</param>
    				<!-- 
    				<param name="allowedTypes">
    					text/plain,application/jpeg
    				</param>
     				-->
    			</interceptor-ref>
    			<interceptor-ref name="defaultStack"></interceptor-ref>
    		</action>
    	</package>
    
    
    

    Action.java

    package test.action;
    
    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UploadFile extends ActionSupport {
    
    	@Override
    	public String execute() throws Exception {
    	
    		System.out.println("UploadFile Action execute");
    		//ServletActionContext.getRequest().getrea.getRealPath("/FileUpload");  //deprecated
    		
    		String pathRoot = ServletActionContext.getServletContext().getRealPath("/FileUpload");
    		File file=new File(pathRoot,myfileFileName );
    		FileUtils.copyFile(myfile,file);
    		return SUCCESS;
    	}
    
    	private String username;
    	private String password;
    	private File myfile;
    
    	private String myfileFileName;
    	private String myfileContextType;
    	
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public File getMyfile() {
    		return myfile;
    	}
    
    	public void setMyfile(File myfile) {
    		this.myfile = myfile;
    	}
    
    	public String getMyfileFileName() {
    		return myfileFileName;
    	}
    
    	public void setMyfileFileName(String myfileFileName) {
    		this.myfileFileName = myfileFileName;
    	}
    
    	public String getMyfileContextType() {
    		return myfileContextType;
    	}
    
    	public void setMyfileContextType(String myfileContextType) {
    		this.myfileContextType = myfileContextType;
    	}
    
    }
    
    

  • 相关阅读:
    跌到哪儿会反弹
    训练看盘能力的方法
    如何设置ListView控件中的列头的颜色!
    市场正在构筑顶部的几个明显征兆
    Managing Unhandled Exceptions in .NET
    Eclipse 3.3.2中配置Visual Editor
    杨建:网站加速系统架构篇
    杨建:网站加速服务器编写篇
    杨建:网站加速实例分析篇
    涨停技术-教你如何捕捉涨停版best
  • 原文地址:https://www.cnblogs.com/wucg/p/1897148.html
Copyright © 2011-2022 走看看