zoukankan      html  css  js  c++  java
  • Sping支持文件的上传和下载

    spring-xml文件配置

    <!-- 文件上传组件 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
    </bean>

    表单页面

    //表单必须用post

    <form action="upLoad" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="upLoadFile" ><br/>
    </br>
    <input type="submit" value="提交"/>
    </form>

    控制器处理

    @Controller
    @RequestMapping("/xw")
    public class XwController {
    @RequestMapping("/upLoad")
    public String upLoad(MultipartFile upLoadFile,HttpSession session) throws IOException{

    //uploadFile和表单中的file的name一致
    if(upLoadFile.getSize()<=0){
    session.setAttribute("message", "未选择文件!");
    return "false";
    }else if(upLoadFile.getSize()>1024*2*1024){
    session.setAttribute("message", "文件过大!");
    return "false";
    }else{
    //获取文件名作为保存到服务器的文件名称
    String fileName=upLoadFile.getOriginalFilename();
    //将上传的文件转成Byte数组
    byte [] bytes = upLoadFile.getBytes();
    //前半部分路径,目录
    String leftPath=session.getServletContext().getRealPath("/WEB-INF/files");
    File file=new File(leftPath);
    if(!file.exists()){
    file.mkdirs();
    }
    File child=new File(file, fileName);
    child.createNewFile();
    FileOutputStream fos=new FileOutputStream(child);
    fos.write(bytes);
    session.setAttribute("message", "上传成功!");
    return "success";
    }
    }
    @RequestMapping("/index")
    public String index(){
    return "index";
    }
    }

  • 相关阅读:
    USACO1.3.3Calf Flac
    USACO1.3.1Mixing Milk
    USACO1.3.2Barn Repair
    USACO2.1.4Healthy Holsteins
    USACO1.5.2Prime Palindromes
    USACO1.4.2The Clocks
    USACO2.1.2Ordered Fractions
    PHP关联数组教程
    你的服务器没有正确响应Token验证的解决方法
    微信公众平台消息接口开发(10)语音触发(非识别)
  • 原文地址:https://www.cnblogs.com/wenwenzuiniucha/p/8625620.html
Copyright © 2011-2022 走看看