struts2上传图片功能实现(单个图片)
2016-03-01 21:20
561 查看
1.搭建struts2框架
a)导入相关的jar包
blank实例中的包即可
b)配置web.xml文件
2.index.jsp中创建表单(注意红色字体)
<form action="Upload_upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file">
<input type="submit" value="上传">
</form>
3.action类(UploadAction)package com.lsw.struts2_upload.actions;
ServletActionContext.getServletContext().getRealPath("/files");
获取的是你项目所在的路径。
a)导入相关的jar包
blank实例中的包即可
b)配置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"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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>c)配置struts2.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 动态调用方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- 开启开发模式,这样可以打印出详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 访问后缀 ,,表示省略不写--> <constant name="struts.actions.extension" value="action,do,,"/> <!-- 当struts.xml文件被修改后自动重新部署 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 允许上传的文件的最大字节数 --> <constant name="struts.multipart.maxSize" value="2097152"/> <!-- 缓存路径 --> <constant name="struts.multipart.saveDir" value="E:\"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="com.lsw.struts2_upload.actions.{1}Action" method="{2}"> <!-- 配置保存路径 --> <param name="savePath">/uploadFile</param> <result name="success">/WEB-INF/pages/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
2.index.jsp中创建表单(注意红色字体)
<form action="Upload_upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file">
<input type="submit" value="上传">
</form>
3.action类(UploadAction)package com.lsw.struts2_upload.actions;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class UploadAction extends ActionSupport{ private File file;//要上传的表单元素的name属性值 private String fileContentType;//上传文件的类型 private String fileFileName;//上传文件的名称 private String savePath;//用来接受依赖注入的文件保存路径 public String upload(){ FileOutputStream fos=null; FileInputStream fis=null; try { fos=new FileOutputStream(getSavePath()+"\\"+getFileFileName()); fis=new FileInputStream(file); byte[] buffer=new byte[1024]; int len=0; while((len=fis.read(buffer))>0){ fos.write(buffer,0,len); } fos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fis!=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return "success"; } 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; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } }
ServletActionContext.getServletContext().getRealPath(savePath):web容器中对于路径都是获取发布项目当前Root下的路径 也就是/跟路径 而非磁盘上任意路径。
ServletActionContext.getServletContext().getRealPath("/files");
获取的是你项目所在的路径。
相关文章推荐
- Myeclipse查看jdk源代码
- Java I/O 模型的演进
- 【eclipse】generate getters and setters错误
- java常见基础知识汇总
- Java--OOP细节(二)
- Introduction to the spring framework(Spring框架简介)
- java学习---控制执行流程
- struts中使用json
- Java遍历文件树的实现和测试
- Java基本数据类型
- Java并发编程:Callable、Future和FutureTask
- eclipse安装pydev
- 算法代码实现之堆排序,Java实现
- 排序算法代码总结-java版
- struts2配置文件struts.xml详解
- 红黑树
- java-获取随机字符串
- [置顶] JavaSE学习笔记_1:Java概述
- Java多线程--Future与Callable
- Can not find the tag library descriptor for "/struts-tags"`