dyl的gravatar头像
dyl2014-05-22 23:25:43

Struts2文件上传配置教程Demo代码下载

项目截图:

Struts2文件上传配置教程Demo代码下载

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>zuidaima</groupId>
  <artifactId>Structs-FileUpLoad</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <url>http://maven.apache.org</url>

	<dependencies>

		<dependency>
			<groupId>com.sun</groupId>
			<artifactId>tools</artifactId>
			<version>1.5.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.1.2</version>
		</dependency>
		
		<dependency>    
	        <groupId>javax.servlet</groupId>    
	        <artifactId>servlet-api</artifactId>    
	        <version>2.5</version>    
	        <scope>provided</scope>    
    	</dependency>

	</dependencies>
  
  
</project>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
	<constant name="struts.multipart.saveDir" value="c:/upload"></constant> 
 	<constant name="struts.action.extension" value="html" />
 	<constant name="struts.devMode" value="true" />
 	<constant name="struts.custom.i18n.resources" value="global" />
 
	<package name="default"  extends="struts-default">
 
 		<action name="index" >
		    <result >/WEB-INF/views/fileupload.jsp</result>
		</action>
 
		<action name="fileUpload"    class="com.zuidaima.fileupload.controller.FileUploadAction">
			<param name="uploadDir">/WEB-INF/upload</param>
		    <result name="SUCCESS">/WEB-INF/views/success.jsp</result>
		</action>
	 
   </package>	
</struts>

FileUploadAction:

package com.zuidaima.fileupload.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author dyl
 *
 */
public class FileUploadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private File fileUpload;
	private String fileUploadContentType;
	private String fileUploadFileName;
	private String uploadDir;

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File file) {
		this.fileUpload = file;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public String execute() throws Exception {
		String newFileName = null;
		long now = System.currentTimeMillis();
		// 得到保存上传文件的目录的真实路径
		File dir = new File(ServletActionContext.getServletContext()
				.getRealPath(uploadDir));
		// 如果该目录不存在,就创建
		if (!dir.exists()) {
			dir.mkdirs();
		}

		// 为避免重名文件覆盖,判断上传文件是否有扩展名,以时间戳作为新的文件名
		int index = fileUpload.getName().lastIndexOf(".");

		if (index != -1) {
			newFileName = now + fileUpload.getName().substring(0,index);
		} else {
			newFileName = Long.toString(now);
		}

		InputStream is = null;
		OutputStream os = null;
		try {
			// 读取保存在临时目录下的上传文件,写入到新的文件中
			is = new FileInputStream(fileUpload);
			os = new FileOutputStream(new File(dir, newFileName));

			byte[] buf = new byte[1024];

			int len = -1;

			while ((len = is.read(buf)) != -1) {
				os.write(buf, 0, len);
			}
			//删除临时文件
			fileUpload.delete();
		} finally {
			is.close();
			os.close();
		}
		return "SUCCESS";
	}

	public String display() {
		return "NONE";
	}
}

项目运行截图:

Struts2文件上传配置教程Demo代码下载

上传后的文件:Struts2文件上传配置教程Demo代码下载

Struts2文件上传配置教程Demo代码下载


最代码官方编辑于2016-8-24 10:07:23


打赏

文件名:FileUpload.zip,文件大小:10.306K下载
  • /
      • /FileUpLoad
        • /FileUpLoad/.classpath
        • /FileUpLoad/.project
          • /FileUpLoad/.settings
            • /FileUpLoad/.settings/.jsdtscope
            • /FileUpLoad/.settings/org.eclipse.jdt.core.prefs
            • /FileUpLoad/.settings/org.eclipse.wst.common.component
            • /FileUpLoad/.settings/org.eclipse.wst.common.project.facet.core.xml
            • /FileUpLoad/.settings/org.eclipse.wst.jsdt.ui.superType.container
            • /FileUpLoad/.settings/org.eclipse.wst.jsdt.ui.superType.name
            • /FileUpLoad/.settings/org.maven.ide.eclipse.prefs
          • /FileUpLoad/WebContent
              • /FileUpLoad/WebContent/META-INF
                • /FileUpLoad/WebContent/META-INF/MANIFEST.MF
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友