spring mvc 框架图片上传、下载案例

spring mvc 图片、音频上传  案列


一、下载   commons-io-2.0.jar  架包(推荐到官网去下载)
       如果没有这个架包,会出现错误
二、在spring 配置文件中添加如下配置:
 <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
     <bean id="multipartResolver"
         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="defaultEncoding" value="UTF-8" />
         <!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
         <property name="maxUploadSize" value="10240000" />
     </bean>

三、在controller层中添加如下方法:
@ResponseBody
    @RequestMapping(value = "/imgUpload", method = RequestMethod.POST)
    public void imgUpload(String id, MultipartFile file,
            HttpServletRequest request, ModelMap model) throws IOException {
        log.info("id = " + id);
        String path = null;// 文件路径
        if (file != null) {
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            log.info("上传原图片的名称=" + fileName);
            type = fileName.indexOf(".") != -1 ? fileName.substring(
                    fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {// 判断文件类型是否为空
                if ("GIF".equals(type.toUpperCase())
                        || "PNG".equals(type.toUpperCase())
                        || "JPG".equals(type.toUpperCase())) {
                    String realPath = "E:\\upload\\img\\"; //
                    File file2 = new File(realPath);
                    if (!file2.exists()) {
                        file2.mkdirs();
                    }
                    log.info("realPath = " + realPath);
                    // 自定义的文件名称
                    String[] names = fileName.split("\\.");
                    // 新图片名称
                    String trueFileName = String.valueOf(System
                            .currentTimeMillis()) + "." + names[1];
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    log.info("存放图片文件的路径:" + path);
                    // 转存文件到指定的路径
                    file.transferTo(new File(path));
                    log.info("文件成功上传到指定目录下");
                } else {
                    log.info("不是我们想要的文件类型,请按要求重新上传");
                }
            } else {
                log.info("文件类型为空");
            }
        } else {
            log.info("没有找到相对应的文件");
        }
        Add add = new Add();
        add.setId(id);
        add.setImgAddress(path);
        boolean str = baseService.update(add);
        log.info("update id=" + id + ",is +" + str);
    }

以上是上传图片的代码,如果要上传音频,只需要在第三个if 判断中加入如下代码:"QLV".equals(type.toUpperCase())即可。

四、然后我们编写一个imgupload.jsp
       <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片文件上传</title>
</head>
<%
String id=request.getParameter("id");

%>
<body>
<fieldset>
<legend>图片上传</legend>
<h5>只能上传单张10M以下的 PNG、JPG、GIF 格式的图片</h5>
<form action="<%=request.getContextPath() %>/userinfo/imgUpload" method="post" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<%=id %>">
        上传图片:<input type="file" name="file">
         <input type="submit" value="提交">
 </form>
 </fieldset>
 </body>
</body>
</html>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值