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>
 

posted @ 2019-04-11 18:17  杰醍奋  阅读(88)  评论(0编辑  收藏  举报