`
李史斌
  • 浏览: 2620 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

struts 2 文件上传

 
阅读更多
1、jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>struts 2 File Upload Test</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<s:form action="fileUpload" method="POST"
			enctype="multipart/form-data">
			<s:file name="myFile" label="文件上传"/><br/>
			<s:textfield name="inputText" label="输入文本"/><br/>
			<s:submit value="提交" />
		</s:form>

	</body>
</html>


2、Action类
package com.jxlg.demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

	private static final int BUFFER_SIZE = 16 * 1024;
	private String savePath; // 保存路径
	private File myFile; // 待上传文件
	private String myFileFileName; // 文件名

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	private String inputText;

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public void setInputText(String inputText) {
		this.inputText = inputText;
	}

	public String getInputText() {
		return inputText;
	}

	private void copyFile(File src, File savePath) {
		try {
			// 原文件
			FileInputStream fis = new FileInputStream(src);
			BufferedInputStream bis = new BufferedInputStream(fis);

			// 文件输出保存
			FileOutputStream fos = new FileOutputStream(savePath);
			BufferedOutputStream bos = new BufferedOutputStream(fos);

			byte buffer[] = new byte[BUFFER_SIZE];
			while (fis.read(buffer) > 0) {
				bos.write(buffer);
			}
			System.out.println("成功上传文件(临时文件)" + src.getName());
			System.out.println("文件存放在:" + savePath.getAbsolutePath());
			bos.close();
			fos.close();
			bis.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public String execute() throws Exception {

		// TODO Auto-generated method stub

		System.out.println("文本输入框输入的文字是:" + getInputText());
		File src = getMyFile();
		Date date = new Date();

		System.out.println("=========filename:" + getMyFileFileName()
				+ "=======contentType:" + getExtendsion());

		//可以设置savePath的值,可以设置服务器的路径
		File saveFilePath = new File(savePath + date.getTime()
				+ getExtendsion());
		System.out.println(saveFilePath.getAbsolutePath());
		copyFile(src, saveFilePath);
		return super.execute();
	}

	public String getExtendsion() {
		int pos = getMyFileFileName().indexOf(".");
		return getMyFileFileName().substring(pos);
	}
}


3、配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="mypackage" extends="struts-default">
		<action name="fileUpload" class="com.jxlg.demo.FileUploadAction">
			<interceptor-ref name="fileUpload">
				<param name="allowedType">image/bmp,image/png,image/gif,image/jpeg</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<param name="savePath">d:\\helloworld\\</param>
			<result name="success">/success_upload.jsp</result>
			<result name="input">/index.jsp</result>
		</action>
	</package>
</struts>    


5、附加的success_upload.jsp
分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2文件上传

    struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2文件上传

    Struts2文件上传

    Struts2 文件上传

    Struts2 文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics