struts2上传图片
来源:互联网 发布:java招聘要求 编辑:程序博客网 时间:2024/05/21 21:36
1.导入jar 包
commons-io-【version】
commons-fileupload-【version】
commons-logging-【version】
freemarker-【version】
log4j-【version】
ognl-【version】
struts2-core-【version】
xwork-core-【version】
2.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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"> <filter> <filter-name>struts2-cleanUp</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts2-cleanUp</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.创建 FileUploadAction
import java.io.BufferedInputStream; public String getMyFileFileName() { public String getFileName() {
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private String savePath;
private String myFileFileName;//获取的文件名
private File myFile;
private String myFileContentType;//获取文件的类型
private String fileName;
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
return myFileFileName;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getSavePath() {
return savePath;
}
public String execute() throws Exception {
//获取文件路径
String path = ServletActionContext.getServletContext().getRealPath(savePath);
File imageFile = new File(path + "//" +myFileFileName);
System.out.println(imageFile);
BufferedInputStream bis = null;//获取一个上传文件的输入流
BufferedOutputStream bos = null;//获取一个上传文件的输出流
try {
bis = new BufferedInputStream(new FileInputStream(myFile));
bos = new BufferedOutputStream(new FileOutputStream(imageFile));
byte buf[] = new byte[(int)myFile.length()];
int length = 0;
while((length = bis.read(buf)) != -1) {
bos.write(buf, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(bis != null) {
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(bos != null) {
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return SUCCESS;
}
}
4.配置 struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="fileUpload" extends="struts-default"> <action name="fileUpload" class="jxust.zjh.action.FileUploadAction" > <param name="savePath">images</param> <result name="success">uploadsuc.jsp</result> </action> </package> </struts>
5,写输入页面 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> </html> <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> </html>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="选择图片"/>
<s:submit value="上传"/>
</s:form>
</body>
6.显示页面
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>My JSP 'uploadsuc.jsp' starting page</title>
</head>
<body>
<s:debug></s:debug>
<h2>上传成功</h2>
<hr>
<img alt="图片" src="<s:property value="savePath + '/'+myFileFileName" />">
</body>
</html>
- struts2 上传 图片
- struts2上传图片
- struts2上传图片
- Struts2上传图片
- struts2图片批量上传
- struts2上传图片
- struts2批量上传图片
- struts2 不能上传图片
- struts2 上传图片
- Struts2上传图片
- struts2上传图片
- struts2上传图片
- struts2 上传图片
- struts2 spring4 图片上传
- struts2图片上传
- struts2 ajax图片上传
- struts2 图片上传 详解
- Struts2上传图片实例
- 一个编译debug版本的mysql的脚本
- C++声明和定义的区别
- 程序员的个人成长
- 证券行业信息系统安全检查经验谈
- VC++的链接错误LNK2005
- struts2上传图片
- LINUX学习脚印(一)
- 修改鼠标光标的形状
- VC++之Edit控件自动换行
- 一个字符串算法
- DC/DC和LDO的区别
- android 中捕获组合键
- ExecuteScalar()方法返回值 及 DBnull和NUll的区别
- JavaScript中的常用算法和函数