1 2 3 4 5 6 7 8 9 | @Path("/filestore") public class WorldCupGroup { … } 清单 3 @GET @Path("filelist") @Produces(MediaType.APPLICATION_JSON) // returns a list of files in JSON format that match the request public JSONObject getFileList(@QueryParam("path") String path) { |
1 2 | GET http://localhost:8080/rest/filestore/filelist?path=C://artifactstore//FIFA World Cup Group |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package com.example.filestore; import java.io.File; import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import com.ibm.json.java.JSONArray; import com.ibm.json.java.JSONObject; @Path("/filestore") public class FIFAWorldCup { public static final String ROOT_DIR = "C:\\artifactstore\\"; // returns a list of files in JSON format that match the request @GET @Path("filelist") @Produces(MediaType.APPLICATION_JSON) public JSONObject getFileList(@QueryParam("path") String path) { JSONObject ret = new JSONObject(); JSONArray filelist = new JSONArray(); JSONObject items = new JSONObject(); if (path == null) { path=this.ROOT_DIR; } /** Method to construct a JSONObject representing the File * as a DataStore item. * * @param file The file contained within rootDir to reference. * * @return JSONObject of the file, or null if file was null or not * contained within the root path of the FileStore. */ private JSONObject createFileItem(String parent,File file) throws IOException { JSONObject item = new JSONObject(); if (parent != null && file != null) { String filePath = file.getAbsolutePath(); //Make sure the request is contained within the root directory if (filePath.startsWith(this.ROOT_DIR)){ item.put("name", file.getName()); item.put("modified", new Long(file.lastModified())); item.put("size", new Long(file.length())); item.put("path", filePath); item.put("directory", new Boolean(file.isDirectory())); // if this is a directory, then add the children if (file.isDirectory()) { File[] child = file.listFiles(); JSONArray children = new JSONArray(); if (child.length > 0) { for (int j = 0; j < child.length; j++) { children.add(child[j].getName()); item.put("children", children); } } else { // since this directory has no children, place an empty child item.put("children",children); } } } } return item; } } // end of class definition |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |