`
退役的龙弟弟
  • 浏览: 446109 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2实现文件上传

 
阅读更多

1.upload.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>
<base href="<%=basePath%>">
</head>
<body>
	<!--  <form action="upload"  method="get">
		文件标题:<input type="text" name="filename"/><br/>
		上传文件:<input type="file" name="file"><br/>
		<input type="submit" value="上传"/><br/>
	</form>-->
	<s:form action="upload1" enctype="multipart/form-data" methos="post"> 
		<s:textfield name="title" label="文件名"></s:textfield><br/>
		<s:file name="upload" label="文件"></s:file><br/>
		<s:submit value="上传"></s:submit><br/>
	</s:form>
</body>
</html>

 

Html代码 
  1. jsp代码:  
  2. <input type="file" id="license" name="license" style="opacity: 0;">  

 

opacity: 0是让input type=”file”全透明,这样用户看不到input type=”file”。层级在文本框和按钮之上。这样用户在点击按钮的时侯实际上点击的input type=”file”;

 

使用js提交form表单时,給form定义id属性,直接$("#id").submit();不需要在有<intput type="submit"/>

 

2.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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="utf-8"/>
    <package name="parameter" namespace="/" extends="struts-default">

        <action name="upload1" class="com.ru.action.UploadAction">
        	<param name="savepath">/upload</param>
        	<!-- 文件上传格式 -->
        	<param name="allowtypes">image/gif,image/x-png,image/jpg,image/jpeg,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param>
            <result name="sucess">/WEB-INF/jsp/sucess.jsp</result>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </action>      
    </package>
</struts>

 

3.UploadAction.java

 

package com.ru.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	
	//title和upload从jsp页面获取。
	private String title;
	private File upload;
	//savePath设置文件的存储路径
	private String savepath;
	//分别获取文件的名字和类型。只要有file类型的参数***就对应这两个属性
	private String uploadFileName; 
	private String uploadContentType;
	//文件过滤属性,通过struts.xml文件配置allowtypes属性值
	private String allowtypes;
	
	
	public String getAllowtypes() {
		return allowtypes;
	}


	public void setAllowtypes(String allowtypes) {
		this.allowtypes = allowtypes;
	}


	public String getUploadContentType() {
		return uploadContentType;
	}


	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}


	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}


	public String getUploadFileName() {
		return uploadFileName;
	}



	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 getSavepath() throws Exception{
		return ServletActionContext.getServletContext().getRealPath("/WEB-INF"+savepath);
	}


	public void setSavepath(String savepath) {
		this.savepath = savepath;
	}

	//过滤文件

	public String filetypes(){
		
		String filetype=getUploadContentType();
		String[] types=getAllowtypes().split(",");
		for(String type:types){
			if(type.equals(filetype)){
				return "ok";
			}
		}
		return "error";
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//System.out.print(ServletActionContext.getServletContext().getRealPath("/WEB-INF"));
		if(filetypes().equals("error")){
			this.addFieldError("uploadfileerror", "上传文件类型错误");
			return "error";
		}
		FileInputStream fis=new FileInputStream(getUpload());
		byte buffer[]=new byte[1024];
		int length=0;
		FileOutputStream fos=new FileOutputStream(getSavepath()+"\\"+getUploadFileName());
		while((length=fis.read(buffer))>0){
			fos.write(buffer, 0, length);
		}
		System.out.print(getUploadContentType());
		return "sucess";
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics