码迷,mamicode.com
首页 > 其他好文 > 详细

struts2上传文件

时间:2014-08-27 23:27:48      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:ssh   图片   java   struts2   

1.upload.java(java文件上传的读写方法)

package com.OS.util;
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.sql.bean.FILE;

import com.opensymphony.xwork2.ActionSupport;

public class upload extends ActionSupport {
	private File picture;
	private String pictureContentType;
	private String pictureFileName;
	FILE file=new FILE();

	public FILE upload() throws Exception{
		//最好保护文件时重命名加时间截
		File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),pictureFileName);
		InputStream ins=null;
		OutputStream ous=null;
		
		try{
		saved.getParentFile().mkdirs();
		ins=new FileInputStream(picture);
		ous=new FileOutputStream(saved);
		
		byte[] b= new byte[1024];
		int len =0;
		while((len=ins.read(b))!=-1){
			ous.write(b,0,len);
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(ous!=null) ous.close();
			if(ins!=null) ins.close();
		}
		file.setFile(picture);
		file.setFileContentType(pictureContentType);
		file.setFileFileName(pictureFileName);
		
      //  BufferedReader bf = new BufferedReader(new StringReader(str));
		return file;
	}
	public File getPicture() {
		return picture;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public String getPictureContentType() {
		return pictureContentType;
	}
	public void setPictureContentType(String pictureContentType) {
		this.pictureContentType = pictureContentType;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

}


2.FILE.java (文件上传类)

package com.sql.bean;

import java.io.File;

public class FILE {
	private File file;
	private String fileContentType;
	private String fileFileName;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	
}

3.uploads.java (action方法)

package com.web.actoin;

import org.apache.struts2.ServletActionContext;

import com.OS.util.upload;
import com.opensymphony.xwork2.ActionSupport;

public class uploads extends ActionSupport{
	public upload u=new upload();
	public String uploadimage() throws Exception{
		System.out.println(".."+ServletActionContext.getRequest().getContextPath()+"/upload/"+u.upload().getFileFileName());
		return "success";
	}
	public upload getU() {
		return u;
	}
	public void setU(upload u) {
		this.u = u;
	}
	
}

4.gMessages.properties(消息文件)

struts.messages.error.uploading = \u6587\u4EF6\u4E0D\u80FD\u4E0A\u4F20\u7684\u901A\u7528\u9519\u8BEF\u4FE1\u606F
struts.messages.error.file.too.large = \u4E0A\u4F20\u6587\u4EF6\u957F\u5EA6\u8FC7\u5927\u7684\u9519\u8BEF\u4FE1\u606F
struts.messages.error.content.type.not.allowed =\u5F53\u4E0A\u4F20\u6587\u4EF6\u4E0D\u7B26\u5408\u6307\u5B9A\u7684contentType

5.struts.properties(struts2资源文件)

struts.custom.i18n.resources=gMessages

6.struts.xml(struts2配置文件)

<?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.i18n.encoding" value="UTF-8"></constant>

<package name="yzm" extends="struts-default">
        <action name="uploadimage" class="com.web.actoin.uploads" method="uploadimage">
     		<interceptor-ref name="fileUpload">
 		  		<param name="savePath">/jsp/</param>  
                <!-- 文件过滤 -->
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <!-- 上传文件大小, 以字节为单位 -->
                <param name="maximumSize">1025956</param>
            </interceptor-ref>
            <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
            <interceptor-ref name="defaultStack" />
		<!-- 异常时返回的界面 -->
    	<result name="input">index.jsp</result>
    	<result name="success">success.jsp</result>
    </action>
</package>
</struts>    

7.index.jsp

<%@ page language="java" import="java.util.*" 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 XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <base href="<%=basePath%>">
    <link href="<%=request.getContextPath() %>/css/init.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="<%=path%>/css/exam.css" />
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>


<form action="uploadimage" enctype="multipart/form-data" method="post" theme = "simple">	
 	<s:file name="u.picture" label="上传视频"  accept=".bmp,.gif,.png,.jpg"></s:file> 
	<s:fielderror name="u.picture">error<s:param></s:param></s:fielderror>
	<s:submit action="uploadimage"  style="width:60px;height:20px;"  value="上传"/>
</form>
  </body>
</html>


struts2上传文件

标签:ssh   图片   java   struts2   

原文地址:http://blog.csdn.net/u010935715/article/details/38880867

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!