Skip to content

Commit 5e31592

Browse files
author
deeper
committed
更改相册页面
1 parent 07fd83c commit 5e31592

File tree

4 files changed

+86
-20
lines changed

4 files changed

+86
-20
lines changed

src/main/java/com/wenjun/oa/action/UploadAction.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
import com.wenjun.oa.bean.Photo;
44
import com.wenjun.oa.bean.User;
55
import com.wenjun.oa.service.UploadService;
6-
import com.wenjun.oa.service.impl.UploadSerivceImpl;
6+
import com.wenjun.oa.tool.TextUtils;
77
import org.apache.commons.io.FileUtils;
88
import org.springframework.context.annotation.Scope;
99
import org.springframework.stereotype.Controller;
1010
import org.springframework.web.bind.annotation.RequestMapping;
1111
import org.springframework.web.multipart.MultipartFile;
1212

1313
import javax.annotation.Resource;
14-
import javax.servlet.ServletContext;
15-
import javax.servlet.http.HttpServletRequest;
1614
import javax.servlet.http.HttpSession;
1715
import java.io.File;
18-
import java.io.FileOutputStream;
1916
import java.io.IOException;
2017
import java.util.List;
2118
import java.util.Map;
19+
import java.util.UUID;
2220

2321
/**
2422
* Created by wangli0 on 2017/4/15.
@@ -36,7 +34,6 @@ public class UploadAction {
3634
@RequestMapping("/uploadUI.action")
3735
public String uploadUI(){
3836

39-
4037
return "upload/uploadUI";
4138
}
4239

@@ -61,12 +58,32 @@ public String ownlist(Map map,HttpSession session){
6158

6259

6360
@RequestMapping("/upload.action")
64-
public String upload(MultipartFile file, HttpSession session) throws IOException {
65-
String dest = session.getServletContext().getRealPath("/WEB-INF/upload");
61+
public String upload(MultipartFile file, HttpSession session,Map map) throws IOException {
62+
63+
User currentUser = (User) session.getAttribute("user");
64+
String name = file.getName();
65+
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
66+
67+
String urlFolder = session.getServletContext().getContextPath()+"/upload/";
68+
String destFolder = session.getServletContext().getRealPath("/upload/");
69+
70+
String fileName = UUID.randomUUID().toString();
71+
72+
String fileDest = destFolder + fileName + suffix;
73+
String imageUrl = urlFolder + fileName + suffix;
74+
6675
System.out.println("文件信息:"+file.getOriginalFilename()+" "+file.getContentType());
6776
// 上传
68-
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(dest+File.separator+file.getOriginalFilename()));
69-
System.out.println("上传成功!");
77+
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(fileDest));
78+
79+
Photo photo = new Photo();
80+
photo.setName(name);
81+
photo.setUserId(currentUser.getId());
82+
photo.setUsername(currentUser.getName());
83+
photo.setUrl(imageUrl);
84+
85+
uploadService.save(photo);
86+
7087
return "redirect:/upload_list.action";
7188
}
7289

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wenjun.oa.tool;
2+
3+
/**
4+
* Created by wangli0 on 2017/4/16.
5+
* github https://github.com/wangli0
6+
* blog http://www.jianshu.com/u/79a88a044955
7+
* website: http://need88.com
8+
*/
9+
public class TextUtils {
10+
public static boolean isEmpty(String text){
11+
if ("".equals(text) || null == text) {
12+
return true;
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
<%--
2-
Created by IntelliJ IDEA.
3-
User: Administrator
4-
Date: 2017/4/15
5-
Time: 10:06
6-
To change this template use File | Settings | File Templates.
7-
--%>
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
82
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
93
<html>
104
<head>
11-
<title>List</title>
5+
<title>精彩相片</title>
6+
<%@ include file="/WEB-INF/jsp/common/adminLTE.jspf" %>
127
</head>
138
<body>
14-
List
9+
10+
11+
12+
<div class="wrapper">
13+
<div class="row">
14+
<div class="col-lg-12">
15+
<section class="panel">
16+
<header class="panel-heading"> 精彩相片
17+
<div class="pull-right"><a href="upload_list_own.action" class="btn btn-warning">我的相片</a>&nbsp;<a href="upload_list.action" class="btn btn-success">全部相片</a>&nbsp;<a href="uploadUI.action" class="btn btn-success">+上传相片</a></div>
18+
</header>
19+
<div class="panel-body">
20+
<div id="gallery" class="media-gal">
21+
<c:forEach var="photo" items="${photoList }">
22+
<div class="images item " >
23+
<a href="${photo.url}" data-lightbox="example-set" data-title="我想知道相片背后的故事">
24+
<img src="${photo.url }" alt="ic_action_share" />
25+
</a>
26+
<p>我想知道相片背后的故事</p>
27+
<p>${photo.username } 上传</p>
28+
29+
</div>
30+
31+
</c:forEach>
32+
33+
</div>
34+
35+
<div class="clearfix"></div>
36+
37+
</div>
38+
</section>
39+
</div>
40+
</div>
41+
</div>
42+
43+
44+
1545
</body>
1646
</html>

src/main/webapp/WEB-INF/jsp/upload/uploadUI.jsp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
<title>文件上传</title>
1212
</head>
1313
<body>
14+
15+
<div>
16+
${error}
17+
</div>
18+
1419
<form action="upload.action" method="post" enctype="multipart/form-data">
15-
<input name="name" placeholder="name" type="text" /><br/>
1620
<input name="file" placeholder="文件上传" type="file"/><br/>
1721
<input type="submit" value="上传"/>
18-
1922
</form>
2023

2124
</body>

0 commit comments

Comments
 (0)