码迷,mamicode.com
首页 > Web开发 > 详细

apache vfs 文件上传

时间:2016-11-06 16:14:29      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:upload   puts   stat   解决中文乱码   art   sts   class   write   encoding   

 1 private static FileSystemManager fsManager = null;
 2     private static final String ftpUri = "ftp://username:password@ip: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 block
43             e.printStackTrace();
44         }catch (FileSystemException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }catch (IOException e) {
48             // TODO Auto-generated catch block
49             e.printStackTrace();
50         }
51     }

 

apache vfs 文件上传

标签:upload   puts   stat   解决中文乱码   art   sts   class   write   encoding   

原文地址:http://www.cnblogs.com/sjcq/p/6035175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!