Struts2框架下实现向服务器上传图片
来源:互联网 发布:eplan 软件 编辑:程序博客网 时间:2024/05/16 09:49
<?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> <constant name="struts.i18n.encoding" value="GBK" /><package name="default" extends="struts-default"><action name="onload" class="com.jht.OnAction"> <!--配置文件上传拦截器,限制上传格式为图片,文件最大为2M--> <interceptor-ref name="fileUpload"><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param><param name="maximumSize">2000000</param></interceptor-ref><interceptor-ref name="defaultStack" /> <!--文件上传后将保存在/upload路径下,upload。jsp是上传页面,onloadpro.jsp是上传成功后的显示页面--> <param name="savePath">/upload</param><result name="success">/onloadpro.jsp</result><result name="input">/upload.jsp</result></action></package></struts>以上为struts.xml文件的核心配置。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html><html> <head> <title>11</title> </head> <body> <span style="color:red"><s:fielderror/></span><form method="post" action="onload.action"enctype="multipart/form-data">文件标题:<input type="text" name="title" /><br>选择文件:<input type="file" name="upload" /><br><input type="submit" value="提交" /></form> </body></html>以上为upload.jsp页面的核心代码。
<%@ page language="java" contentType="text/html;charset=GBK"%><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>上传成功</title></head><body> <center> 上传成功!<br> 文件标题:<s:property value=" + title" /><br> 文件为:<br> <img src="<s:property value="'upload/'+uploadFileName"/>"/><br> </center></body></html>以上为onloadpro.jsp页面的核心代码。
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class OnAction extends ActionSupport{private String title;private File upload;private String uploadContentType;private String uploadFileName;private String savePath;private String getSavePath() throws Exception {return ServletActionContext.getRequest().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}@Overridepublic String execute() throws Exception {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;}}以上为Action类的核心代码。
0 0
- Struts2框架下实现向服务器上传图片
- Struts2框架下使用Kindeditor上传图片
- Java框架Struts2实现图片上传
- 向服务器上 上传图片
- Base64Coder方式实现android向jsp服务器后台上传图片
- laravel5.1框架下实现图片上传
- 利用struts2框架实现当用户上传图片到服务器后,再显示到前台页面中
- struts2 上传图片到服务器
- struts2之如何向服务器上传文件
- 利用 Javascript 实现上传图片并向服务器获取图片路径后显示的 demo
- Android中向服务器上传图片
- 用afn向服务器上传图片
- xhEditor struts2实现图片上传
- kindeditor图片上传 struts2实现
- uploadifive struts2实现图片上传
- uploadify struts2实现图片上传
- uploadifive struts2实现图片上传
- struts2实现图片上传功能
- 浅析Spring IOC(控制反转)
- 网页设计前端的表单记录
- apache和iis发布网站防火墙设置
- PMP十大知识领域(47个项目管理过程)
- 用类创建链队列
- Struts2框架下实现向服务器上传图片
- uva156 Ananagrams (stl map的使用)
- HTML基础5--浮动练习
- awk控制语句
- C/C++ 与函数有关的typedef用法
- Ruby元编程笔记<一>
- hdu2112 HDU Today(dijkstra+map)
- LabVIEW中局部变量、全局变量和引用及属性节点的相关
- android onActivityResult不执行的问题