• Struts2之上传


    单文件上传

    上传页面

    <%@ 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="upload.action" method="post" enctype="multipart/form-data">
    	文件:<input type="file" name="upload"><br><br>
    	上传者:<input type="text" name="author">
    	<input type="submit" value="上传">
    </form>
    </body>
    </html>
    

    struts.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <constant name="struts.devMode" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	<action name="upload" class="cn.jiangwenwen.action.UploadAction" method="fileUpload">
    		<result name="success">/success.jsp</result>
    	</action>
    </package>
    </struts>
    

    Action类

    public class UploadAction  extends ActionSupport{
    	
    	//存放上传的文件对象
    	private File upload;	
    	//上传的文件名称
    	private String uploadFileName;
    	//上传的上传者
    	private String author;
    	
    	public String fileUpload() throws IOException {
    		
    		FileInputStream fis = new FileInputStream(upload);
    		
    		String path = "D://pic/"+uploadFileName;
    		
    		FileOutputStream fos = new FileOutputStream(path);
    		
    		int flag = 0;
    		
    		while((flag=fis.read())!=-1) {
    			fos.write(flag);
    		}
    		fis.close();
    		fos.close();
    		
    		return SUCCESS;
    		
    	}
    
    	public File getUpload() {
    		return upload;
    	}
    
    	public void setUpload(File upload) {
    		this.upload = upload;
    	}
    
    	public String getUploadFileName() {
    		return uploadFileName;
    	}
    
    	public void setUploadFileName(String uploadFileName) {
    		this.uploadFileName = uploadFileName;
    	}
    
    	public String getAuthor() {
    		return author;
    	}
    
    	public void setAuthor(String author) {
    		this.author = author;
    	}
    	
    }
    

    成功接收页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" isELIgnored="false"%>
    <!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>
    <!-- 此 /pic为服务器配置的路径-->
    <img src="/pic/${uploadFileName }">
    </body>
    </html>
    

    批量上传

    上传页面

    <%@ 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="upload.action" method="post" enctype="multipart/form-data">
    	文件:<input type="file" name="upload"><br><br>
    		<input type="file" name="upload"><br><br>
    
    	上传者:<input type="text" name="author">
    	<input type="submit" value="上传">
    </form>
    </body>
    </html>
    

    struts.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <!-- 修改文件上传大小限制 -->
    <constant name="struts.multipart.maxSize" value="11111111111111111"></constant>
    <!-- 开启国际化,value的值是配置文件的名称(在src目录下)-->
    <constant name="struts.custom.i18n.resources" value="message" />
    <!-- 开启开发者模式 -->
    <constant name="struts.devMode" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	<action name="upload" class="cn.jiangwenwen.action.UploadAction" method="upload">
    		<result name="success">/success.jsp</result>
    		<result name="input">/error.jsp</result>
    		<!-- 控制单个文件上传大小 -->
    		<interceptor-ref name="fileUpload">
    			<param name="maximumSize">
    			1500
    			</param>
    		</interceptor-ref>
    		 <interceptor-ref name="defaultStack"></interceptor-ref> 
    	</action>
    </package>
    </struts>
    

    Action类

    public class UploadAction  extends ActionSupport{
    	//上传的文件对象
    	private File[] upload;
    	//上传的文件名称
    	private String[] uploadFileName;
    	//上传的文件类型
    	private String[] uploadContentType;
    	
    	public String upload() {
    		
    		String path = "D://pic/";
    		
    		for(int i=0;i<upload.length;i++) {
    			upload[i].renameTo(new File(path,uploadFileName[i]));
    			System.out.println("上传成功");
    		}
    		
    		return SUCCESS;
    	}
    
    	public File[] getUpload() {
    		return upload;
    	}
    
    	public void setUpload(File[] upload) {
    		this.upload = upload;
    	}
    
    	public String[] getUploadFileName() {
    		return uploadFileName;
    	}
    
    	public void setUploadFileName(String[] uploadFileName) {
    		this.uploadFileName = uploadFileName;
    	}
    
    	public String[] getUploadContentType() {
    		return uploadContentType;
    	}
    
    	public void setUploadContentType(String[] uploadContentType) {
    		this.uploadContentType = uploadContentType;
    	}
    }
    

    成功接收页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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:debug></s:debug>
    </body>
    </html>
    
  • 相关阅读:
    【转】Uiautomator Api浅析
    【转】UiAutomator简要介绍
    后台自动启动appium
    adb通过wifi连接Android设备
    Python字符串处理和输入输出
    OJ题目输出的生成
    Weka的使用和二次开发(朴素贝叶斯及其属性选择)
    PointNet++论文理解和代码分析
    VGG-16复现
    二维偏序-最长上升子序列的两种求解方式
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9465385.html
Copyright © 2020-2023  润新知