• Struts2文件上传


    package com.bjsxt.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class Upload2Action extends ActionSupport {
    	
    	private String title;
    	
    	private File upload;  //封装上传文件域
    	private String uploadContentType; //封装上传文件类型
    	private String uploadFileName; //封装上传文件名
    	
    	private String savePath;
    	
    
    	public String execute() throws Exception{
    		System.out.println(uploadFileName);
    		System.out.println(uploadContentType);
    		
    		FileOutputStream fos = new FileOutputStream(getSavePath()+getUploadFileName());
    		FileInputStream fis = new FileInputStream(upload);
    		byte[] buf = new byte[1024];
    		int len = 0;
    		while((len = fis.read(buf))>0){
    			fos.write(buf, 0, len);
    		}
    		
    		fos.close();
    		fis.close();
    		
    		return SUCCESS;
    		
    	}
    
    	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 savePath;
    	}
    
    	public void setSavePath(String savePath) {
    		this.savePath = savePath;
    	}
    
    	
    }
    

      

    <action name="Upload" class="com.bjsxt.test.Upload2Action">
            	<param name="savePath">d:/temp/</param> <!-- 可以通过这种方式在配置文件中设置action属性的值 -->
                <result name="input">/uploadFile.jsp</result>
                <result name="success">/upload_ok.jsp</result>
            </action>
    

      

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <form action="Upload" enctype="multipart/form-data" method=post >
            文件标题:<input type=text name=title /> <br/>
            选择文件:<input type=file name=upload /><br/>
            <input type=submit value=上传 />
        </form>
  • 相关阅读:
    zlib 2.1.8 编译遇到的问题以及解决方法
    Golang简单日志类
    Golang获得执行文件的当前路径
    Golang的session管理器
    cocos2dx spine之二 :spine变色
    cocos2dx spine之一 :spine缓存 (c++ & lua)
    动态规划
    动态规划
    数学
    [Offer收割]编程练习赛3
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/4437562.html
Copyright © 2020-2023  润新知