图片上传

图片上传测试:

1、pojo

 1 package com.ssm.pojo;
 2 
 3 public class NewsPic {
 4     /*
 5      * 
 6     新闻图片
 7 
 8 CREATE TABLE NEWS_PIC(
 9     id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '图片id(编号)',
10     categoryId BIGINT(20) NOT NULL COMMENT '图片的父id,属于哪个新闻的图(此处应该传过来一个新闻的id)',
11     address VARCHAR(255) NOT NULL COMMENT '图片地址',
12     NAME VARCHAR(255) NOT NULL COMMENT '图片名称(旧名称)',
13     TXT VARCHAR(255) COMMENT '图片描述',
14     created DATETIME DEFAULT NULL COMMENT '创建时间',
15     updated DATETIME DEFAULT NULL COMMENT '修改时间',
16     PRIMARY KEY(id)
17 )ENGINE=INNODB DEFAULT CHARSET=UTF8 COMMENT='新闻信息的图片,每一条新闻可能有一张或多张图片'
18      */
19     
20     private long id;
21     private long categoryId;
22     private String address;
23     private String name;
24     private String text;
25     private String created;
26     private String updated;
27     public long getId() {
28         return id;
29     }
30     public void setId(long id) {
31         this.id = id;
32     }
33     public long getCategoryId() {
34         return categoryId;
35     }
36     public void setCategoryId(long categoryId) {
37         this.categoryId = categoryId;
38     }
39     public String getAddress() {
40         return address;
41     }
42     public void setAddress(String address) {
43         this.address = address;
44     }
45     public String getName() {
46         return name;
47     }
48     public void setName(String name) {
49         this.name = name;
50     }
51     public String getText() {
52         return text;
53     }
54     public void setText(String text) {
55         this.text = text;
56     }
57     public String getCreated() {
58         return created;
59     }
60     public void setCreated(String created) {
61         this.created = created;
62     }
63     public String getUpdated() {
64         return updated;
65     }
66     public void setUpdated(String updated) {
67         this.updated = updated;
68     }
69     
70     
71 }

2、mapper.xml文件

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
 3 <mapper namespace="com.ssm.dao.NewsPicMapper">
 4     <insert id="addNewsPic" parameterType="com.ssm.pojo.NewsPic">
 5         
 6         INSERT 
 7             INTO 
 8         NEWS_PIC(
 9             categoryId,
10             address,
11             name,
12             text,
13             created,
14             updated    
15         )VALUES(
16             #{categoryId},
17             #{address},
18             #{name},
19             #{text},
20             #{created},
21             #{updated}
22         )
23     </insert>
24 </mapper>

 

3、dao

 1 package com.ssm.dao;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.springframework.stereotype.Repository;
 6 
 7 import com.ssm.pojo.NewsPic;
 8 
 9 @Repository
10 public interface NewsPicMapper {
11     
12     /**
13      * 
14       *<p>Title: addNewsPic</p>  
15       *<p>Description: 添加一张新闻图</p>  
16       * @param newsPic
17      */
18     void addNewsPic(NewsPic newsPic);
19     
20 }

 

4、serviceImpl

 1 package com.ssm.service.Imp;
 2 
 3 import javax.annotation.Resource;
 4 import javax.transaction.Transactional;
 5 
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.ssm.dao.NewsPicMapper;
 9 import com.ssm.pojo.NewsPic;
10 import com.ssm.service.NewsPicService;
11 import com.utils.TouGuResult;
12 @Service
13 @Transactional
14 public class NewsPicServiceImpl implements NewsPicService{
15 
16     @Resource
17     private NewsPicMapper picMapper;
18     
19     /**
20      * 添加一张新闻图
21      */
22     @Override
23     public TouGuResult addNewsPic(NewsPic newsPic) {
24         picMapper.addNewsPic(newsPic);
25         return TouGuResult.ok(newsPic);
26     }
27 
28 }

5、Controller


 1 package com.ssm.controller;
 2 
 3 import java.io.File;
 4 import java.net.InetAddress;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Date;
 7 
 8 import javax.annotation.Resource;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.bind.annotation.ResponseBody;
16 import org.springframework.web.multipart.MultipartFile;
17 
18 import com.ssm.pojo.NewsPic;
19 import com.ssm.service.NewsPicService;
20 import com.utils.IDUtils;
21 import com.utils.PicUtils;
22 import com.utils.TouGuResult;
23 
24 @Controller
25 @RequestMapping("/newsPic")
26 public class NewsPicController {
27     
28     @Resource
29     private NewsPicService newsPicService;
30     
31     @RequestMapping("/add")
32     @ResponseBody
33     public TouGuResult addNewsPic(@RequestParam MultipartFile[] multipartFiles,String categoryId,HttpServletRequest request,HttpServletResponse response) throws Exception{
34         if(multipartFiles.length !=0 && !multipartFiles.equals("")){    
35             String newName = null;
36             String pic_path = null;
37             for (MultipartFile multipartFile : multipartFiles) {
38                 String oldName = multipartFile.getOriginalFilename();
39                 if(oldName != null && oldName.length() != 0){
40                     newName = IDUtils.genImageName() + oldName;
41                     pic_path = PicUtils.NEWS_PIC;
42                     //写到内存中 
43                     File file = new File(pic_path + "\\" + newName);
44                     //写到磁盘上
45                     multipartFile.transferTo(file);
46                     //构建存储在数据库中的地址
47                     
48                     InetAddress addr = InetAddress.getLocalHost();  
49                     String ip=addr.getHostAddress().toString(); 
50                     pic_path = request.getScheme() +"://"+ ip+":"+request.getServerPort()+PicUtils.NEWS_SQL_PIC+newName;
51                     //存到数据库中
52                     NewsPic newsPic = new NewsPic();
53                     newsPic.setCategoryId(Long.valueOf(categoryId));
54                     newsPic.setName(oldName);
55                     newsPic.setAddress(pic_path);
56                     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57                     newsPic.setCreated(simpleDateFormat.format(new Date()));
58                     newsPic.setUpdated(simpleDateFormat.format(new Date()));
59                     newsPicService.addNewsPic(newsPic);
60                     return TouGuResult.ok(newsPic);
61                 }else{
62                     return TouGuResult.build(500, "文件名称不能为空");
63                 }
64             }
65             return TouGuResult.ok();
66         }else{
67             request.getRequestDispatcher("/null.jsp").forward(request, response);
68             return null;
69         }
70 
71         
72     }
73 }
 

6、同一返回实体类TouGuResult

 1 package com.utils;
 2 
 3 import java.util.List;
 4 
 5 public class EUDataGridResult {
 6     private long total;
 7     private List<?> rows;
 8     public long getTotal() {
 9         return total;
10     }
11     public void setTotal(long total) {
12         this.total = total;
13     }
14     public List<?> getRows() {
15         return rows;
16     }
17     public void setRows(List<?> rows) {
18         this.rows = rows;
19     }
20 }

7、图片存储地址工具类:

 1 package com.utils;
 2 
 3 public class PicUtils {
 4     
 5     /**
 6      * NEWS_PIC:新闻图在服务器上存储的地址
 7      */
 8     public static final String NEWS_PIC = "D:\\AllImage\\news";
 9     
10     /**
11      * NEWS_SQL_PIC:新闻图在数据库中存储的地址
12      */
13     public static final String NEWS_SQL_PIC = "/news/";
14     
15 }

 

 

下一篇介绍图片上传时自动返回主键的方法

 

posted @ 2018-08-14 11:26  没有名字~  阅读(291)  评论(0编辑  收藏  举报