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