博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2上传文件
阅读量:6300 次
发布时间:2019-06-22

本文共 4055 字,大约阅读时间需要 13 分钟。

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\u606Fstruts.messages.error.file.too.large = \u4E0A\u4F20\u6587\u4EF6\u957F\u5EA6\u8FC7\u5927\u7684\u9519\u8BEF\u4FE1\u606Fstruts.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配置文件)

/jsp/
image/bmp,image/png,image/gif,image/jpeg
1025956
index.jsp
success.jsp
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+"/";%>          
error

转载于:https://www.cnblogs.com/yutingliuyl/p/6784826.html

你可能感兴趣的文章
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
Springmvc SimpleMappingExceptionResolver
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>
Java使用AES加密解密时需要的Jar
查看>>
我的友情链接
查看>>
JVM的JMX
查看>>
我的友情链接
查看>>
做人与做技术
查看>>
oracle列转行函数 LISTAGG()
查看>>
发段代码玩玩
查看>>
Linux_free(buffer与cache区别)
查看>>
我的友情链接
查看>>
一分钟学会spring注解之@value注解
查看>>
CentOS7修改网卡为eth0
查看>>
Exchange 2007 数据保护与灾难恢复
查看>>
Oracle在线调整redo日志组数及组成员
查看>>
快速搜索工具Everything
查看>>
反射遍历所有属性,包括父类
查看>>
Canvas、Path 和 Paint 实例 (游戏开发必备)
查看>>
Sentry for Impala Without kerberos(CDH 5.10)
查看>>