zoukankan      html  css  js  c++  java
  • MogliFS与spring mvc结合简单示例

    一、MogliFS 与Spring结合配置请参照上文

    二、上传页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data">
        file1:<input type="file"  name="myFile"/>
        <br>
        file2:<input type="file"  name="myFile"/>
        <br>
        <input type="submit" value="上传">
    </form>

    三、mvc的CommonsMultipartResolver解析器配置

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="104857600"/>
    </bean>

    四、上传

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
          public String upload(            
                  HttpServletRequest request, @RequestParam(value = "myFile", required = false) MultipartFile[] files) {
                  try {
                      for(int i=0;i<files.length;i++){
                          MultipartFile  file=files[i];
                          
                          if(StringUtils.isNotEmpty(file.getName()) && file.getSize() > 0){
                              
                              ......
                              
                              MojiFile mf = moji.getFile("MyFileKey"+uuid);
                              
                              OutputStream out = null;
                              InputStream in = null;
                              try {
                                  out = mf.getOutputStream();
                                  in = file.getInputStream();
                                  
                                  byte[] bs = new byte[1024];
                                  while( in.read(bs) != -1){
                                      out.write(bs);
                                  }
                                  
                                  out.flush();
                              } finally {
                                  in.close();
                                  out.close();
                              }
                          }
                      }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                  return "success";
          }

    五、下载

    @RequestMapping(value = "download", method = RequestMethod.GET)
        public  void download(HttpServletRequest request,
                HttpServletResponse response, String fileName) throws Exception {
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            
         ...... MojiFile mf = moji.getFile("MyFileKey" + uuid); response.setHeader("Content-disposition", "attachment; filename=" + new String(newFileName.getBytes("gb2312"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(mf.length())); bis = new BufferedInputStream(mf.getInputStream()); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); }
  • 相关阅读:
    搭建Jfroum过程记录
    性能测试见解1
    线程与进程
    性能测试见解3需求分析
    软件功能测试的用例设计总结
    hadoop 2.x安装:不能加载本地库 解决libc.so.6 version GLIBC_2.14 not found问题
    hadoop 2.x安装:不能加载本地库 java.library.path错误
    linux使用:CentOS安装jdk
    hadoop 2.x安装:完全分布式安装
    hadoop 2.x安装:不能加载本地库 重新编译hadoop本地库
  • 原文地址:https://www.cnblogs.com/xiaoliangup/p/9606934.html
Copyright © 2011-2022 走看看