在struts2框架做上传功能
来源:互联网 发布:淘宝宝贝过期不存在 编辑:程序博客网 时间:2024/06/09 15:53
上传一张图片
1.index.jsp首页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <form action="file1" enctype="multipart/form-data" method="post"> 姓名:<input name="name" /></br> 年龄:<input name="age" /></br> 性别:<input name="sex" /></br> 照片:<input name="photo" type="file" /></br> <input name="submit" type="submit" /></br> </form> </body></html>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="default" extends="struts-default" namespace="/"><action name="file1" class="cn.action.FileAction" method="execute"><param name="savePath">/upload</param><result>succ.jsp</result></action></package></struts>
3.FileAction.java
package cn.action;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.UUID;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class FileAction {private String name;private Integer age;private String sex;private File photo;private String photoFileName;private String savePath;//在显示页面的需要的地址private String imgSrc;public String execute(){//下两行获取路径,savepath在xml中要配置ServletContext context=ServletActionContext.getServletContext();String realPath=context.getRealPath(savePath);//查看是否存在文件路径File saveDir=new File(realPath);if(!saveDir.exists()){saveDir.mkdir();}//随机数,做图片名String saveFileName = UUID.randomUUID().toString();//获得图片格式String ext = photoFileName.substring(photoFileName.lastIndexOf("."));//上传到的目的地的地址String outFilePath = realPath + "/" + saveFileName + ext;this.imgSrc = this.savePath + "/" + saveFileName + ext;// ȥ�� ��ͷ�� / this.imgSrc = imgSrc.substring(1);try {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(photo));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath));int tmp = -1;while( (tmp = bis.read()) != -1 ){bos.write(tmp);}bos.flush();bos.close();bis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return Action.SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public File getPhoto() {return photo;}public void setPhoto(File photo) {this.photo = photo;}public String getPhotoFileName() {return photoFileName;}public void setPhotoFileName(String photoFileName) {this.photoFileName = photoFileName;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}public String getImgSrc() {return imgSrc;}public void setImgSrc(String imgSrc) {this.imgSrc = imgSrc;}}
4.上传成功后显示页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'succ.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> 姓名:${name } <br> 年龄:${age }<br/> 性别:${sex }<br/> 照片:<img src="${imgSrc }" /> </body></html>
0 0
- 在struts2框架做上传功能
- 在struts2框架下做下载功能
- 【Struts2框架】第八节上传-利用struts2实现上传功能
- 在含有Struts2框架的应用中添加KindEditor上传图片功能
- struts2-上传功能
- struts2文件上传功能
- struts2框架上传研究
- 文件上传-Struts2框架
- Struts2 框架文件上传
- 在struts2框架中使用servlet做接口
- 在struts2框架上上传多张图片
- struts2+xheditor+上传图片功能
- Struts2文件上传功能开发
- struts2实现文件批量上传功能
- struts2实现文件上传功能
- struts2实现图片上传功能
- Struts2框架的文件上传
- struts2框架-------文件上传机制
- matlab获取目录中图像名称及路径的递归实现
- LeetCode:Product of Array Except Self
- BestCoder Round #62 (div.2)Clarke and five-pointed star(极角排序,判断五边形)
- ECMAScript 继承机制实现
- 怎么在win10系统下安装ubuntu双系统
- 在struts2框架做上传功能
- Android手机获取屏幕分辨率(屏幕高度和宽度)
- linux命令之eval
- Swift(二十、扩展)
- iOS中 百度地图详解 韩俊强的博文
- ECMAScript 定义类或对象
- xshell 登陆 ubuntu
- 学习
- 简单的ACE反应器(Reactor)模式服务器