package com.jsfp.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.jsfp.model.User;
import com.jsfp.service.UserService;
import com.jsfp.test.ResponseDataWrapper;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 添加用户信息
* @param name
* @param birthday
* @param gender
* @param email
* @param img
* @return
*/
@RequestMapping("/insert")
public String insert(@RequestParam(value = "name") String name,
@RequestParam(value = "birthday") String birthday,
@RequestParam(value = "gender") String gender,
@RequestParam(value = "email") String email,
@RequestParam(value = "img") MultipartFile img) {
if (!img.isEmpty()) {
// 使用StreamsAPI方式拷贝文件
try {
//获取图片名称
String imgName=img.getOriginalFilename();
//截取图片的格式
String str=imgName.substring(imgName.indexOf("."),imgName.length());
//获取UUID
String id = UUID.randomUUID().toString();
String image=id+str;
Streams.copy(img.getInputStream(), new FileOutputStream(
"D:/img/" + image), true);
User user = new User();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
user.setBirthday(date);
user.setEmail(email);
user.setGender(gender);
user.setImg("D:/img/" + image);
user.setName(name);
boolean con = userService.insertUser(user);
if (con) {
return "redirect:/user/getAll";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "redirect:/user/getAll";
}
/**
* 查询全部用户信息
* @param session
* @return
*/
@RequestMapping("/getAll")
public String getAll(HttpSession session) {
List<User> list = userService.getAllUser();
session.setAttribute("list", list);
return "users/getAll";
}
/**
* 根据id查询用户信息
* @param id
* @param session
* @return
*/
@RequestMapping("/getUserById")
public String getUserById(@RequestParam(value = "id") int id,HttpSession session) {
User user = userService.getUserById(id);
session.setAttribute("user", user);
return "users/getUserById";
}
/**
* 删除用户信息
* @param id
* @return
*/
@RequestMapping("/deleteUser")
public String deleteUser(@RequestParam(value = "id") int id){
boolean con=userService.deleteUser(id);
return "redirect:/user/getAll";
}
/**
* 修改头像(假修改)
* @param img
* @return
*/
@RequestMapping("/update")
@ResponseBody
public ResponseDataWrapper update(@RequestParam(value = "img") MultipartFile img){
ResponseDataWrapper rdw=new ResponseDataWrapper();
if (!img.isEmpty()) {
// 使用StreamsAPI方式拷贝文件
try {
//获取图片名称
String imgName=img.getOriginalFilename();
//截取图片的格式
String str=imgName.substring(imgName.indexOf("."),imgName.length());
//获取UUID
String id = UUID.randomUUID().toString();
String image=id+str;
Streams.copy(img.getInputStream(), new FileOutputStream(
"D:/img/" + image), true);
User user = new User();
user.setImg("D:/img/" + image);
rdw.setObj(user);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return rdw;
}
/**
* 修改用户头像(真修改)
* @param img
* @param id
* @return
*/
@RequestMapping("/updateUser")
@ResponseBody
public ResponseDataWrapper updateUser(@RequestParam(value = "img")String img,
@RequestParam(value = "id")int id){
ResponseDataWrapper rdw=new ResponseDataWrapper();
User user=new User();
user.setId(id);
user.setImg(img);
boolean con=userService.updateUser(user);
if (con) {
rdw.setCode(1);
rdw.setRandom(id);
} else {
rdw.setCode(0);
}
return rdw;
}
/**
* 修改用户头像获取头像信息
* @param id
* @param response
* @return
*/
@RequestMapping("/getImgs")
public String getImgs(@RequestParam(value = "img") String img,HttpServletResponse response){
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
is=new FileInputStream(img);
bis=new BufferedInputStream(is);
os=response.getOutputStream();
bos=new BufferedOutputStream(os);
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();// 这里一定要调用flush()方法
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 用户详细信息页面获取头像信息
* @param id
* @param response
* @return
*/
@RequestMapping("/getImg")
public String getImg(@RequestParam(value = "id") int id,HttpServletResponse response){
User user = userService.getUserById(id);
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
is=new FileInputStream(user.getImg());
bis=new BufferedInputStream(is);
os=response.getOutputStream();
bos=new BufferedOutputStream(os);
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();// 这里一定要调用flush()方法
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java图片(头像)上传Demo
共106个文件
jar:58个
xml:9个
java:7个
需积分: 47 78 下载量 174 浏览量
2017-04-20
17:27:44
上传
评论 1
收藏 25.69MB ZIP 举报
温馨提示
这是我做的一个简易用户上传头像的Demo,是仿照qq修改头像的效果做的,内附数据库,使用SSM框架搭建(项目里有一些无用的jar包,希望大家不要介意)。包括添加用户时的头像上传,以及修改用户头像(使用ajax上传头像并及时刷新显示)。欢迎大家来下载
资源推荐
资源详情
资源评论
收起资源包目录
java图片(头像)上传Demo (106个子文件)
UserController.class 9KB
ResponseDataWrapper.class 2KB
User.class 2KB
UserServiceImpl.class 1KB
test.class 778B
UserMapper.class 495B
UserService.class 372B
.classpath 884B
org.eclipse.wst.common.component 584B
org.eclipse.wst.jsdt.ui.superType.container 49B
layer.css 11KB
layer.ext.css 3KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
xmlbeans-2.3.0.jar 2.54MB
guava-18.0.jar 2.15MB
jaxb-xjc-2.2.6.jar 1.98MB
aspectjweaver-1.5.4.jar 1.81MB
druid-1.0.4.jar 1.75MB
jsf-impl.jar 1.15MB
jaxb-impl-2.2.6.jar 1.06MB
jackson-databind-2.4.4.jar 1.03MB
swetake-qrcode-0.1.jar 1014KB
freemarker-2.3.20.jar 995KB
spring-core-3.2.2.RELEASE.jar 846KB
spring-context-3.2.2.RELEASE.jar 842KB
mysql-connector-java-5.1.10-bin.jar 707KB
mybatis-3.2.7.jar 696KB
jxl.jar 664KB
spring-webmvc-3.2.2.RELEASE.jar 621KB
spring-web-3.2.2.RELEASE.jar 612KB
spring-beans-3.2.2.RELEASE.jar 593KB
joda-time-2.3.jar 568KB
xstream-1.4.7.jar 519KB
core-2.2.jar 508KB
log4j-1.2.17.jar 478KB
spring-test-3.2.2.RELEASE.jar 445KB
jstl-1.2.jar 405KB
fastjson-1.2.5.jar 402KB
spring-jdbc-3.2.2.RELEASE.jar 396KB
spring-orm-3.2.2.RELEASE.jar 378KB
spring-aop-3.2.2.RELEASE.jar 328KB
spring.net.sf.cglib-2.2.0.jar 319KB
jsf-api.jar 314KB
commons-lang-2.6.jar 278KB
commons-httpclient-3.0.jar 273KB
commons-codec-1.7.jar 254KB
spring-tx-3.2.2.RELEASE.jar 236KB
jackson-core-2.4.4.jar 220KB
spring-expression-3.2.2.RELEASE.jar 191KB
wsdl4j-1.6.1.jar 145KB
XmlSchema-1.4.5.jar 143KB
spring-context-support-3.2.2.RELEASE.jar 125KB
aspectjrt-1.5.4.jar 112KB
jaxb-api-2.2.6.jar 97KB
commons-io-1.3.2.jar 86KB
jsonrpc.jar 73KB
neethi-3.0.1.jar 69KB
commons-fileupload-1.3.jar 67KB
commons-pool.jar 61KB
activation.jar 54KB
commons-logging.jar 52KB
spring-aspects-3.2.2.RELEASE.jar 50KB
mybatis-spring-1.2.2.jar 48KB
jacob.jar 47KB
javase-2.2.jar 39KB
jackson-annotations-2.4.0.jar 38KB
slf4j-api-1.7.7.jar 29KB
stax-api-1.0.1.jar 26KB
slf4j-log4j12-1.7.7.jar 9KB
xmlpull-1.1.3.1.jar 7KB
aopalliance-1.0.jar 4KB
UserController.java 7KB
ResponseDataWrapper.java 1KB
UserServiceImpl.java 1KB
User.java 905B
UserMapper.java 378B
UserService.java 311B
test.java 229B
jquery.min.js 90KB
layer.js 15KB
layer.ext.js 5KB
.jsdtscope 503B
getUserById.jsp 4KB
getAll.jsp 1KB
insert.jsp 808B
index.jsp 411B
MANIFEST.MF 39B
org.eclipse.wst.jsdt.ui.superType.name 6B
icon.png 11KB
icon-ext.png 6KB
org.eclipse.jdt.core.prefs 364B
.project 1KB
jdbc.properties 170B
jdbc.properties 170B
user.sql 1KB
applicationContext.xml 3KB
applicationContext.xml 3KB
spring-servlet.xml 2KB
共 106 条
- 1
- 2
资源评论
孤独与烈酒
- 粉丝: 3
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功