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(); }

 

posted @ 2018-09-07 20:43  小亮的BLOG  阅读(296)  评论(0)    收藏  举报