apache vfs 檔案上傳

來源:互聯網
上載者:User

標籤:upload   puts   stat   解決中文亂碼   art   sts   class   write   encoding   

 1 private static FileSystemManager fsManager = null; 2     private static final String ftpUri = "ftp://username:[email protected]:port/dir/";  3        4       static 5       { 6         try 7         { 8           FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance(); 9           FileSystemOptions options = new FileSystemOptions();10           //解決中文亂碼11           builder.setControlEncoding(options, "UTF-8");12           builder.setServerLanguageCode(options, "zh");13           fsManager = VFS.getManager();14         }15         catch (FileSystemException e)16         {17           e.printStackTrace();18         }19       }20 21 @RequestMapping({"/doUpload"})22       public void doUpload(HttpServletRequest request, HttpServletResponse response, Model model) {23          try {24             if ((request instanceof MultipartHttpServletRequest)) {25                 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;26                  if (multipartRequest.getFiles("uploadFile").size() > 0) {27                      //得到傳入的第一個檔案,這裡只是為了測試28                      MultipartFile file = (MultipartFile)multipartRequest.getFiles("uploadFile").get(0);29                      String fileName = file.getOriginalFilename();30                      String filePath = ftpUri+fileName;31                     //解決中文亂碼問題32                     filePath = new String(filePath.getBytes("UTF-8"),"ISO-8859-1");33                     FileObject fileObject = fsManager.resolveFile(filePath);34                     //如果檔案不存在,則建立檔案35                     if(!fileObject.exists()) {36                         fileObject.createFile();37                     }38                     IOUtils.write(file.getBytes(), fileObject.getContent().getOutputStream());39                  }40             }41         } catch (UnsupportedEncodingException e) {42             // TODO Auto-generated catch block43             e.printStackTrace();44         }catch (FileSystemException e) {45             // TODO Auto-generated catch block46             e.printStackTrace();47         }catch (IOException e) {48             // TODO Auto-generated catch block49             e.printStackTrace();50         }51     }

 

apache vfs 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.