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

    1.环境要求

    commons-fileupload-xxx.jar
    commons-io-xxx.jar

    2.准备jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!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>
    <s:form action="Fileupload" method="" enctype="multipart/form-data">
    <s:textfield name="title" label="用户名"/><br>
    <s:file name="upload" label="上传文件"/><br>
    <s:submit name="submit" value="上传" />
    </s:form>
    </body>
    </html>


    3.Action页面

    package com.icss.action.upload;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
    ***************************
    *@类名  UploadAction.java
    *@作者  jatpeo
    *@日期  2017年11月7日 下午2:42:32
    *@版本  v1.0
    *@描述	文件上传
      
    ***************************
    */
    public class UploadAction extends ActionSupport{
    	
    	//1.普通字段name属性名
    	private String title;  
    	private File upload;//file 控件名           它要和表单name属性和action属性名要一致
    	//2.控件名+ContentType
    	private String uploadContentType;//上传文件的类型
    	//3.控件名+FileName
    	private String uploadFileName;//上传文件的名称
    	//4.上传文件的路径 upload
    	private String savePath; //获取完整的路径
    	
    	//上传
    	public String execute() throws Exception {
    		byte[] buffer = new byte[2048];//缓冲区
    		//输入流
    		FileInputStream fis = new FileInputStream(upload);
    		//输出流     
    		FileOutputStream fos = new FileOutputStream(getSavePath()+"\"+uploadFileName);
    		int len=fis.read(buffer);
    		while(len>0){
    			fos.write(buffer, 0, len);
    			len=fis.read(buffer);
    		}
    		fos.flush();
    		fos.close();
    		fis.close();
    		return "success";
    
    	}
    	
    	
    	
    	
    	
    	
    	
    	//生成get和set方法
    	public String getTitle() {
    		return title;
    	}
    	public void setTitle(String title) {
    		this.title = title;
    	}
    	public File getUpload() {
    		return upload;
    	}
    	public void setUpload(File upload) {
    		this.upload = upload;
    	}
    	public String getUploadContentType() {
    		return uploadContentType;
    	}
    	public void setUploadContentType(String uploadContentType) {
    		this.uploadContentType = uploadContentType;
    	}
    	public String getUploadFileName() {
    		return uploadFileName;
    	}
    	public void setUploadFileName(String uploadFileName) {
    		this.uploadFileName = uploadFileName;
    	}
    	public String getSavePath() {
    		return ServletActionContext.getServletContext().getRealPath(savePath);
    	}
    	public void setSavePath(String savePath) {
    		this.savePath = savePath;
    	}
    	
    	
    	
    	
    	
    	
    	
    }
    

    4.配置struts.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.devMode" value="false"/>
      <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
      <constant name="struts.configuration.xml.reload" value="true"/>
    <package name="upload" namespace="/" extends="struts-default">
    <!-- 单文件上传 -->
    <action name="Fileupload" class="com.icss.action.upload.UploadAction">



    </package>
    </struts>
     

  • 相关阅读:
    为了快一点为什么却要慢一点
    大数与小数的求和算法
    Tips for newbie to read source code
    学习Data Science/Deep Learning的一些材料
    Git Sophisticated Commands
    两套JRE
    Java environment variables and their functionality
    Change Git Default Editor in Windows
    Multiton & Singleton
    Java Synchronized Blocks
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767546.html
Copyright © 2011-2022 走看看