当前位置: 代码迷 >> J2SE >> SpringMVC做文件上传怎么在工程里找到这个图片?
  详细解决方案

SpringMVC做文件上传怎么在工程里找到这个图片?

热度:41   发布时间:2016-04-24 01:06:05.0
SpringMVC做文件上传如何在工程里找到这个图片??
File tmp = new File("/upload/voice/",multipartFile.getOriginalFilename());
if(!tmp.exists()){
tmp.getParentFile().mkdirs(); 
tmp.createNewFile(); 
}
multipartFile.transferTo(tmp);

这是上传的代码,我再root下面也建了/upload/voice/文件夹,可是上传成功了里面却没有文件,怎样让文件能传到在root下/upload/voice/??????求解.....

------解决方案--------------------
File tmp = new File("/upload/voice/",multipartFile.getOriginalFilename());
 
这个multipartFile并没有上传到你的服务器的文件夹上,要上传你还少做了点东西,看看这个
Java code
//得到文件?名   String filename=file.getOriginalFilename();           25.               if(file.getSize()>0){                      try {           SaveFileFromInputStream(file.getInputStream(),"D:/",filename);       } catch (IOException e) {           System.out.println(e.getMessage());           return null;       }   }else{       throw new Exception("上传失败:上传文件不能为?空");   } public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException   {             FileOutputStream fs=new FileOutputStream( path + "/"+ filename);       byte[] buffer =new byte[1024*1024];       int bytesum = 0;       int byteread = 0;        while ((byteread=stream.read(buffer))!=-1){           bytesum+=byteread;           fs.write(buffer,0,byteread);           fs.flush();       }        fs.close();       stream.close();         }
  相关解决方案