铃木一彻演过什么:struts2实现文件上传
来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 02:35:11
index.jsp 上传页面<%@page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
${requestScope.typeError}
成共上传页面success.jsp<%@ page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
上传成功!
文件标题:
文件为:
" />
参看tomcat的web.xml中的mime:
gif
image/gif
在src目录下创建struts.propertiesstruts.locale=zh_CN
struts.i18n.encoding=GBK
struts.multipart.parser=jakarta
struts.xml文件
value="globalMessages" />
image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg
/upload
/success.jsp
/index.jsp
action方法package lee;import java.io.*;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
// 接受依赖注入的属性
private String savePath; // 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
} private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
} public void setTitle(String title) {
this.title = title;
} public void setUpload(File upload) {
this.upload = upload;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getTitle() {
return (this.title);
} public File getUpload() {
return (this.upload);
} public String getUploadContentType() {
return (this.uploadContentType);
} public String getUploadFileName() {
return (this.uploadFileName);
} @Override
public String execute() throws Exception {
System.out.println("开始上传单个文件---");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
// 判断是否允许上传
String filterResult = filterType(this.getAllowTypes().split(","));
// if (filterResult != null) {
// ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
// return filterResult;
// }
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return SUCCESS;
} public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
} public String getAllowTypes() {
return allowTypes;
} public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
}
xml配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp
struts-cleanup
/*
index.jsp
程序可运行
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
${requestScope.typeError}
成共上传页面success.jsp<%@ page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
上传成功!
文件标题:
文件为:
参看tomcat的web.xml中的mime:
struts.i18n.encoding=GBK
struts.multipart.parser=jakarta
struts.xml文件
image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg
/upload
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
// 接受依赖注入的属性
private String savePath; // 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
} private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
} public void setTitle(String title) {
this.title = title;
} public void setUpload(File upload) {
this.upload = upload;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getTitle() {
return (this.title);
} public File getUpload() {
return (this.upload);
} public String getUploadContentType() {
return (this.uploadContentType);
} public String getUploadFileName() {
return (this.uploadFileName);
} @Override
public String execute() throws Exception {
System.out.println("开始上传单个文件---");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
// 判断是否允许上传
String filterResult = filterType(this.getAllowTypes().split(","));
// if (filterResult != null) {
// ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
// return filterResult;
// }
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return SUCCESS;
} public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
} public String getAllowTypes() {
return allowTypes;
} public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
}
xml配置文件
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
程序可运行
struts2实现文件上传
struts2文件上传
struts2多文件上传
Struts2上传文件-2
Spring中实现文件上传
Struts2下多文件的上传与下载
Struts21实现文件上传图片,并显示
Struts2中有关文件详解
在Struts 2中实现文件上传 - Max On Java - BlogJava
免费上传文件网站!
免费上传文件网站!.
在ASP.NET中实现多文件上传 (作者:孟宪会 阅读:111246 发表于:2003-05-20 23:41:07 )
如何上传网站文件(详解)
如何上传Flash(swf)文件到博
如何上传Flash(swf)文件到博客.
迅雷是如何偷偷上传文件的?
一些可以免费上传文件的网站
struts2中使用kindeditor上传图片(包括jmagic压缩图片) - 鬼屋里的...
怎样上传大容量文件-,---文件分割的方法
怎样上传大容量文件----文件分割的方法
virtualbox实现文件共享,window
putty来实现上传和下载 | PuTTY中文站
如何上传Flash(swf)文件到博客-------ldm2124086 -----
禁止迅雷偷偷上传已下载的文件.