struts文件上传

阅读更多

1.配置web.xml(上一篇有)

 

2.配置struts.xml



    
    	
		
		
   		
    

 3. FileAction  类

package com.file.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport{

	private File uploadFile;
	private String uploadFileContentType;
	private String uploadFileFileName;
	
	public String file(){
		System.out.println("11111111111111111111");
		String realPath = "e:\\现计/piture";
		File file  = new File(realPath);
		if(!file.exists()){
			file.mkdirs();
		}
		try {
			FileUtils.copyFile(uploadFile, new File(file, uploadFileFileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); 
			return "input";
		}
		return "success";
	}

	/**********get 和 set方法************/
	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
}

 4.配置 file.xml



    
    	

    	
    		
    			/success.jsp
    			/default.jsp
    		
    	
    

 5.jsp页面访问

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




文件上传


文件上传

 

你可能感兴趣的:(struts,文件上传)