知识积累-ssm实现图片上传

摘要:终于开始了自己的第一次CSDN博客,虽然以前有过这种想法,但是一直没有付诸于行动,在这次的安卓实训中因为需要上传图片,虽然自己早已经掌握了利用ssm框架上传图片的知识,但是由于很久没有用到图片上传的知识,导致知识的遗忘,因此在用到时不得不去搜索其他的博客,大大的浪费了我的时间,因此今天开启自己的第一次博客之旅,将平时的知识做一个积累,让以后用到的时候可以比较方便的直接使用,提高开发效率。

目录:
【一】、单张图片的上传
1.添加maven依赖
2.定义文件上传解析器
3.jsp页面
4.controller层
【二】、多张图片的上传
1.jsp代码
2.contoller层

一、单张图片的上传:
开发流程:
1.因为我后台使用的是ssm框架,所有在图片上传上可以使用到SpringMvc的文件上传解析器,所以首先我们在maven里面添加maven依赖。

 <!--数据上传的包-->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.1</version>
</dependency>

2.在springMvc的配置文件中定义文件上传解析器。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--设定默认编码-->
    <property name="defaultEncoding" value="UTF-8"/>
    <!--设定文件上传最大值-->
    <property name="maxUploadSize" value="5242880"/>
    <property name="maxInMemorySize" value="454544096"/>
</bean>

3.实现图片上传的jsp页面的核心代码:

 <%--注意在这里要将form属性的enctype="multipart/form-data"写上才能将数据解析为二进制的格式--%>
<form action="${pageContext.request.contextPath}/news/uploadImage" enctype="multipart/form-data" method="post">
    <input type="text" name="news_title"><br/>
    <input type="file" name="file"><br/>
    <input type="submit" value="上传">
</form>

4.最后就是controller层的代码了,在controller层我们会用到MultipartFile来得到文件。

 @ResponseBody
 @CrossOrigin  //允许跨域请求
 @RequestMapping(value = "/uploadImage", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
 public String upoladImage(HttpServletRequest request, String news_tilte,MultipartFile file) throws IOException {
    //获取文件的名字,注意这里的file要和MultipartFile 所定义的file一样,而  MultipartFile 所定义的file要和jsp中选择图片的name属性值相同。
    String fileName = file.getOriginalFilename();
    //获取服务器的路径
    String realPath = request.getSession().getServletContext().getRealPath("/images/newsImage/");
    //将文件写入到服务器
    file.transferTo(new File(realPath + fileName));
    //返回数据,这是自己写的工具类返回json数据
    CorrectJsonObject correctJsonObject = new CorrectJsonObject("上传成功");
    return JSONObject.toJSONString(correctJsonObject);

}
这样就基本将单张图片的上传搞定了。

二、多张图片的上传:
1.多张图片的上传和单张图片上传都差不多,在添加maven依赖和配置文件解析器都是一样的步骤,所以我们从jsp代码说起,jsp代码其实也差不多,但是为了可以同时选中多张图片,我们将jsp的input的属性修改一下,上传多张图片的jsp的核心代码。

<form action="${pageContext.request.contextPath}/news/uploadImage" enctype="multipart/form-data" method="post">
<input type="text" name="news_title"><br/>
<%--将multiple属性值设为multiple可以实现同时选中多张图片--%>
<input type="file" name="file" multiple="multiple"><br/>
<input type="submit" value="上传">
</form>

2.controller层的代码

@ResponseBody
@CrossOrigin
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
public String upoladImage(HttpServletRequest request,MultipartFile[] file) throws IOException {

    //多张图片上传
    //获取服务器路径
    String realPath = request.getSession().getServletContext().getRealPath("/images/newsImage/");
    //对上传的多张图片进行遍历
    for (MultipartFile f : file) {
        //获取图片的名字
        String fileName = f.getOriginalFilename();

        //判断文件后缀名
        if (!fileName.endsWith(".jpeg") && !fileName.endsWith(".PNG") && !fileName.endsWith(".jpg")) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(104, "禁止上传此类型文件");
            return JSONObject.toJSONString(errorJsonObject);
        }
        //将文件写入到服务器
        f.transferTo(new File(realPath+fileName));
        //判断文件是否已经写入到服务器
        File fil = new File(realPath + fileName);
        if (!fil.exists()) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(105, "上传图片失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

}
至此对于springmvc上传一张或者多张图片就应该可以用了,初次写博客,如有不当请多包涵。

猜你喜欢

转载自blog.csdn.net/FreedomOfTheWind/article/details/82561513