上传时500错误,还有文件下载时的相关jar包记得倒全。
一开始报错以为路径不对
org.csource.common.MyException: item "tracker_server" in /D:/IADA_workspace/FastDfsTest/target/FastDfsTest-1.0-SNAPSHOT/WEB-INF/classes/config.conf not found后后来发现是配置文件中我加了前缀fastdfs.去掉就好了直接是tracker_server
还有记得配置服务器端storage.conf的tracker地址必须是公网,还有nginx-fdfs的也是,不然会连接超时
文件下载
*/ public String fileDownLoad(String group,String filename){ System.out.println("downLoad............"); System.out.println("group : "+group); System.out.println("filename : "+filename); try { byte[] bytes = storageClient1.download_file(group,filename); System.out.println(bytes == null); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); int i = 0; FileOutputStream os = new FileOutputStream("D://11.doc"); while ((i = bais.read())!=-1){ os.write(i); } os.flush(); os.close(); bais.close(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return "success"; } Controller @Controller public class FileController { @RequestMapping(value = "/upload",method = {RequestMethod.POST}) public String upload(@RequestParam(value = "file", required = false) MultipartFile file){ System.out.println("jinlaile"); try{ FastFile fastFile = new FastFile(file.getBytes(),file.getOriginalFilename(),file.getSize()); String path = FileUtil.upload(fastFile); System.out.println("success"+" "+path); return "redirect:"; } catch (IOException e) { e.printStackTrace(); } return null; } @RequestMapping("/download") @ResponseBody public String fileDownLoad(String group,String filename) { System.out.println("downLoad............"); System.out.println("group : " + group); System.out.println("filename : " + filename); FileUtil fileutil = new FileUtil(); fileutil.fileDownLoad(group,filename); return "success"; } }springmvc配置文件 <context:component-scan base-package="Controller" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5120000"/> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/Jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>配置文件config.conf ## fastdfs-client.properties connect_timeout_in_seconds = 5 network_timeout_in_seconds = 30 charset = UTF-8 http_anti_steal_token = false http_secret_key = FastDFS1234567890 http_tracker_http_port = 80 tracker_server =101.200.63.29:22122