Java整合fastDFS上传文件需要配置springmvc配置文件及其它事项

xiaoxiao2021-02-28  24

上传时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的也是,不然会连接超时

pojo:FastFile
public class FastFile implements Serializable{ public static final long serialVersion = 2637755431406080379L; //文件二进制 private byte[] content; //文件名称 private String name; //文件长度 private long size; public FastFile() { } public FastFile(byte[] content, String name, long size) { this.content = content; this.name = name; this.size = size; } public static long getSerialVersion() { return serialVersion; } public byte[] getContent() { return content; } public void setContent(byte[] content) { this.content = content; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } } FileUtil public class FileUtil implements Serializable { private static final long serialVersionUID = -4462272673174266738L; private static TrackerClient trackerClient; private static TrackerServer trackerServer; private static StorageClient1 storageClient1; static { try { //clientGloble读配置文件 ClassPathResource resource = new ClassPathResource("config.conf"); System.out.println(resource.getClassLoader().getResource("config.conf").getPath()); ClientGlobal.init(resource.getClassLoader().getResource("config.conf").getPath()); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageClient1 = new StorageClient1(trackerServer, null); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } /** * 文件上传 */ public static String upload(FastFile fastFile) { String path = null; try { //文件扩展名 String ext = FilenameUtils.getExtension(fastFile.getName()); //meta list是表文件的格式 NameValuePair[] matalist = new NameValuePair[3]; matalist[0] = new NameValuePair("fileName", fastFile.getName()); matalist[1] = new NameValuePair("fileExt", ext); matalist[2] = new NameValuePair("fileSize", String.valueOf(fastFile.getSize())); path = storageClient1.upload_file1(fastFile.getContent(), ext, matalist); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return path; } /**

文件下载

*/ 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
转载请注明原文地址: https://www.6miu.com/read-2632771.html

最新回复(0)