Struts2将图片输出到页面
一、思路
二、代码
<td class="tdBg" width="200px">头像:</td>
<td>
<!-- 显示头像 -->
<img src="${basePath}nsfw/user_showHeadImg.action?user.id=${user.id}" width="100" height="100"/>
<input type="file" name="headImg" accept = "image/*"/>
</td>
<td class="tdBg" width="200px">头像:</td>
<td>
<!-- 显示头像 -->
<img src="${basePath}nsfw/user_showHeadImg.action?user.id=${user.id}" width="100" height="100"/>
<input type="file" name="headImg" accept = "image/*"/>
</td>
package com.tax.web.user;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.tax.pojo.nsfw.User;
import com.tax.service.UserService;
/**
* UserAction
* @author ZENG.XIAO.YAN
* @date 2017年7月11日 上午10:06:05
* @version v1.0
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 4526496105243102063L;
@Autowired
private UserService userService;
private User user;
/** 文件上传的3个属性 */
private File headImg; // 这个名字和表单的name的值一样
private String headImgFileName;
private String headImgContentType;
/** 存放图片的本地文件夹 */
private static final String USER_IMAGE_DIR = "D:/upload";
/**
* 展示用户头像 Action方法
* @return 将头像输出到页面
* @see 访问方式:tax/nsfw/user_showHeadImg.action?user.id=xxxx
*/
public String showHeadImg() {
// 这个user的id是通过前台传过来的
if(null != user && user.getId() != null) {
// 通过用户id去数据库查找出用户头像的地址
String img = userService.findById(user.getId()).getHeadImg();
if(StringUtils.isNotBlank(img)) {
// 拼接成本地地址,如:D:/upload/user/0dc14d2b81444ce1b5600a3fe43f9f30.jpg
// USER_IMAGE_DIR = D:/upload
// img 如:user/0dc14d2b81444ce1b5600a3fe43f9f30.jpg
File imgFile = new File(USER_IMAGE_DIR + "/" + img);
// 如果图片文件存在,就输出到页面
if(imgFile.exists()) {
/** 获取HttpServletResponse */
HttpServletResponse response = ServletActionContext.getResponse();
/** 设置响应的内容类型 */
response.setContentType("images/jpeg");
/** 以下3行代码用于设置禁止浏览器缓存该图片 */
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Prama", "no-cache");
// 以下为IO流操作
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(imgFile));
// 这个Response.getOutputStream()是用于输出到浏览器的输出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关流
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
// 这里没有返回视图,直接返回NONE
return NONE;
}
/**
* 专门用于文件上传的方法,返回文件路径
* @return 文件路径
*/
private String uploadFile() {
try {
if (null != headImg) {
// 获取存放文件夹路径
// USER_IMAGE_DIR = D:/upload
String prePath = USER_IMAGE_DIR.concat("/user");
if(!new File(prePath).exists()) {
new File(prePath).mkdirs();
}
// 新的文件名
String fileName = UUID.randomUUID().toString().replaceAll("-", "")
.concat(headImgFileName.substring(headImgFileName.lastIndexOf(".")));
// 用common-io.jar的工具copy文件
FileUtils.copyFile(headImg, new File(prePath,fileName));
return "user/".concat(fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** setter and getter method */
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public File getHeadImg() {
return headImg;
}
public void setHeadImg(File headImg) {
this.headImg = headImg;
}
public String getHeadImgFileName() {
return headImgFileName;
}
public void setHeadImgFileName(String headImgFileName) {
this.headImgFileName = headImgFileName;
}
public String getHeadImgContentType() {
return headImgContentType;
}
public void setHeadImgContentType(String headImgContentType) {
this.headImgContentType = headImgContentType;
}
}
package com.tax.web.user;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.tax.pojo.nsfw.User;
import com.tax.service.UserService;
/**
* UserAction
* @author ZENG.XIAO.YAN
* @date 2017年7月11日 上午10:06:05
* @version v1.0
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 4526496105243102063L;
@Autowired
private UserService userService;
private User user;
/** 文件上传的3个属性 */
private File headImg; // 这个名字和表单的name的值一样
private String headImgFileName;
private String headImgContentType;
/** 存放图片的本地文件夹 */
private static final String USER_IMAGE_DIR = "D:/upload";
/**
* 展示用户头像 Action方法
* @return 将头像输出到页面
* @see 访问方式:tax/nsfw/user_showHeadImg.action?user.id=xxxx
*/
public String showHeadImg() {
// 这个user的id是通过前台传过来的
if(null != user && user.getId() != null) {
// 通过用户id去数据库查找出用户头像的地址
String img = userService.findById(user.getId()).getHeadImg();
if(StringUtils.isNotBlank(img)) {
// 拼接成本地地址,如:D:/upload/user/0dc14d2b81444ce1b5600a3fe43f9f30.jpg
// USER_IMAGE_DIR = D:/upload
// img 如:user/0dc14d2b81444ce1b5600a3fe43f9f30.jpg
File imgFile = new File(USER_IMAGE_DIR + "/" + img);
// 如果图片文件存在,就输出到页面
if(imgFile.exists()) {
/** 获取HttpServletResponse */
HttpServletResponse response = ServletActionContext.getResponse();
/** 设置响应的内容类型 */
response.setContentType("images/jpeg");
/** 以下3行代码用于设置禁止浏览器缓存该图片 */
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Prama", "no-cache");
// 以下为IO流操作
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(imgFile));
// 这个Response.getOutputStream()是用于输出到浏览器的输出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关流
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
// 这里没有返回视图,直接返回NONE
return NONE;
}
/**
* 专门用于文件上传的方法,返回文件路径
* @return 文件路径
*/
private String uploadFile() {
try {
if (null != headImg) {
// 获取存放文件夹路径
// USER_IMAGE_DIR = D:/upload
String prePath = USER_IMAGE_DIR.concat("/user");
if(!new File(prePath).exists()) {
new File(prePath).mkdirs();
}
// 新的文件名
String fileName = UUID.randomUUID().toString().replaceAll("-", "")
.concat(headImgFileName.substring(headImgFileName.lastIndexOf(".")));
// 用common-io.jar的工具copy文件
FileUtils.copyFile(headImg, new File(prePath,fileName));
return "user/".concat(fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** setter and getter method */
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public File getHeadImg() {
return headImg;
}
public void setHeadImg(File headImg) {
this.headImg = headImg;
}
public String getHeadImgFileName() {
return headImgFileName;
}
public void setHeadImgFileName(String headImgFileName) {
this.headImgFileName = headImgFileName;
}
public String getHeadImgContentType() {
return headImgContentType;
}
public void setHeadImgContentType(String headImgContentType) {
this.headImgContentType = headImgContentType;
}
}
Struts2将图片输出到页面的更多相关文章
- ASP.NET 画图与图像处理-如何直接输出到页面
有时候我们生成的图片并不需要保存到磁盘中,而是直接输出到页面,比如验证码.实时报表等,如何做呢?请参考如下: protected void Page_Load(object sender, E ...
- 使用IExport进行图片输出出现File creation error
使用IExport进行图片输出(.JPG)时,出现如下异常File creation error. 在ESRI.ArcGIS.Output.ExportJPEGClass.FinishExport ...
- js引入php 用来加载静态页面 输出到页面中
HTML页面中加入代码 <script type="text/javascript" src="http://www.域名.com/js.php?id=tjyd&q ...
- 动态jsp页面转PDF输出到页面
最近工作中遇到不少问题.总结一下.这段代码主要功能是将一个生成JSP页面转发成PDF输出到页面 需要利用ITEXT String html = ServletUtils.forward(request ...
- JavaScript-2.2 document.write 输出到页面的内容
<html> <head> <meta http-equiv="content-type" content="text/html;chars ...
- Struts2如何传值到jsp页面
Struts2如何传值到jsp页面 不是action传值到jsp页面,而是jsp页面获取action中的属性值,或者范围(如request,session,application等)里的值.所以,有两 ...
- 【PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异常】
public static void main(String[] args) throws IOException { /** * PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异 ...
- jstl-按照html的形式输出至页面
一.按照html的形式输出至页面 <c:out value="${xxx}" default="默认值" escapeXml="false&qu ...
- webpack2.0 css文件引入错误解决及图片输出在根目录配置问题
webpack引入css文件,main.js内容如下 import Vue from 'vue'; import App from './App.vue'; import Mint from 'min ...
随机推荐
- 使用Tomcat部署应用
概述 一个简单的web项目下载地址:https://files.cnblogs.com/files/Mike_Chang/hello.rar Tomcat部署应用三种方法. 方法一 将一个WAR文件或 ...
- Expo大作战(十八)--expo如何发布成独立应用程序,打包成apk或者ipa,发布到对应应用商店
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- SQL2005的SSMS连接SQL2012会有问题
SQL2005的SSMS连接SQL2012会有问题 如果用SQL2005的SSMS连接SQL2012会产生“索引数组越界”的问题并且使用不了 反过来使用SQL2012的SSMS连接SQL2005就没有 ...
- Prometheus Node_exporter 之 Network Netstat TCP
Network Netstat TCP /proc/net/snmp 1. TCP Segments type: GraphUnit: shortLabel: Segments out (-) / i ...
- MVC 上传下载
在Asp.net的WEBform中,上传文件与下载文件处理是很简单的事情,如果转为ASP.NET MVC呢?那就没有那么容易了,难少少,也不是很难,一起来看下本文吧.本文主要讲如何在Asp.net M ...
- 向Sql Server数据库插入中文时显示乱码的解决办法 (转)
转自:http://blog.csdn.net/wizardlun/article/details/4577658 參考:http://shareideas.blog.51cto.com/362642 ...
- Python学习---Python环境变量安装问题0907
问题背景: 重新安装操作系统后,原来的环境变量丢失[因Python3.5安装目录是E盘,文件还在,只是丢失了环境变量而已,添加即可] 问题解决: 方法一:使用cmd命令添加path环境变量 在cmd下 ...
- 【C语言】 使用Beep() 函数 演奏歌曲
#include <windows.h> int main(){ ){ Beep(, ); Beep(, ); Beep(, ); Beep(, ); Beep(, ); Beep(, ) ...
- October 24th, 2017 Week 43rd Tuesday
We can't give up trying. The fight was worth it. 我们不能放弃尝试,奋斗是值得的. When doing researches in some cutt ...
- XtraEditors四、TextEdit、ButtonEdit、PictureEdit、RadioGroup、PopupContainerEdit
TextEdit控件 以文本框的形式绑定各种形式的选择框: 文本框设置 输入 密码 字符 时, 要有 * 号掩盖输入的字符, 代码如下: textEdit1.Properties.PasswordCh ...