struts2.1.6实现(图片)文件的上传

1.首先创建一个FileUploadAction
package jxust.zjh.action;
import java.io.BufferedInputStream;
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;
}

public String getMyFileFileName() {
return myFileFileName;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}

public String getFileName() {
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;
}
}
2.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>
</web-app>
3.配置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>
4.建一个上传页面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
</html>
5.上传成功后转向的页面uploadsuc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>
<s:debug></s:debug>
<h2>上传成功</h2>
<hr>
<img alt="图片" src="<s:property value="savePath + '/'+myFileFileName" />">
</body>
</html>
运行时服务器控制台有如下输出 Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
原因是struts.multipart.saveDir没有配置。struts.multipart.saveDir用于指定存放临时文件的文件夹,该配置写在struts.properties文件中。
可以在struts.properties文件加入如下代码解决:
struts.multipart.saveDir = /images
:上传成功的图片并不存放在myeclipse的workspace项目中,而是在tomcat中webapps下对应的项目中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值