Java's new project learning online notes-day8

Keywords: Java Database MongoDB JSON

1.1.3 API interface 1.1.3.1 model class
The file information of the system (information of small files such as pictures and documents) is stored in mongodb, and the model class of file information is below.
1) the model is as follows:
[mw_shl_code=applescript,true]
file‐size‐threshold: 0

Temporary directory of uploaded files

location:

Maximum supported file size

 max‐file‐size: 1MB  
 # Maximum support request size   
 max‐request‐size: 30MB xuecheng:   fastdfs:   
  connect_timeout_in_seconds: 5   
  network_timeout_in_seconds: 30  
   charset: UTF‐8   
  tracker_servers: 192.168.101.64:22122
@Data @ToString @Document(collection = "filesystem") public class FileSystem {   
    @Id  
   private String fileId;   
  //File request path    
private String filePath;   
  //file size    
private long fileSize;    
//File name 
    private String fileName;   
//file type   
  private String fileType; 
    //image width  
   private int fileWidth;  
   //Picture height  
   private int fileHeight;   
  //User id, used for authorizing temporary use   
  private String userId; 
    //Business key    
private String businesskey;    
//Business Tags 
    private String filetag;  
  //File meta information   
  private Map metadata;  
}
[/mw_shl_code]

Explain:
"leId: the file ID returned by fastDFS. "lePath": request fastDFS to browse file URL. "Let tag": File tag. Since the file system service is a public service, the file system service assigns a file tag to the subsystem using the file system service to identify which system this file comes from.
businesskey: a business identification field provided by file system service for other subsystems. Each subsystem uses it according to its own needs. For example, course management stores course id in this field to identify which course the picture belongs to. metadata: meta information about the file.
2) collection creates the database XC ﹣ fs (file system database) in mongodb, and creates the collection fi lesystem.


1.1.3.2 Api interface
Under the API project, create the com.xuecheng.api.u lesystem package,

[mw_shl_code=applescript,true] 

public interface FileSystemControllerApi {      
   /**    
  * Upload files   
   * @param multipartFile file  
    * @param filetag file label 
     * @param businesskey Business key 
     * @param metedata Meta information, json format  
    * @return  
    */  
   public UploadFileResult upload(MultipartFile multipartFile,     
                String filetag,        
             String businesskey,             
        String metadata); }[/mw_shl_code]

1.1.2.3 Dao
The file information is stored in the database, mainly the file path in the file system.

[mw_shl_code=applescript,true]public interface FileSystemRepository extends MongoRepository<FileSystem,String> {   }[/mw_shl_code]
1.1.2.4 Service 
[mw_shl_code=applescript,true]@Service public class FileSystemService {       private static final Logger LOGGER = LoggerFactory.getLogger(FileSystemService.class);     
  @Value("${xuecheng.fastdfs.tracker_servers}")  
   String tracker_servers;    
@Value("${xuecheng.fastdfs.connect_timeout_in_seconds}")   
  int connect_timeout_in_seconds;   
  @Value("${xuecheng.fastdfs.network_timeout_in_seconds}")   
  int network_timeout_in_seconds;   
  @Value("${xuecheng.fastdfs.charset}")   
  String charset;  
     @Autowired  
   FileSystemRepository fileSystemRepository;   
    //Load the configuration of fdfs    
private void initFdfsConfig(){  
       try {      
       ClientGlobal.initByTrackers(tracker_servers);    
         ClientGlobal.setG_connect_timeout(connect_timeout_in_seconds);   
          ClientGlobal.setG_network_timeout(network_timeout_in_seconds);        
     ClientGlobal.setG_charset(charset);     
    } catch (Exception e) {   
          e.printStackTrace();       
      //Error initializing file system         
    ExceptionCast.cast(FileSystemCode.FS_INITFDFSERROR);     
    }   
  }   
  //Upload files  
   public UploadFileResult upload(MultipartFile file,     
                               String filetag,        
                            String businesskey,       
                             String metadata){    
       if(file == null){       
      ExceptionCast.cast(FileSystemCode.FS_UPLOADFILE_FILEISNULL);     
    }
        //Upload file to FDFS [/ MW ﹣ SHL ﹣ code]
[mw_shl_code=applescript,true]String fileId = fdfs_upload(file);   
      //Create file information object     
    FileSystem fileSystem = new FileSystem();      
   //File id       
  fileSystem.setFileId(fileId);   
      //The path of the file in the file system     
    fileSystem.setFilePath(fileId);  
       //Business identification    
     fileSystem.setBusinesskey(businesskey);    
     //Label       
  fileSystem.setFiletag(filetag);    
     //metadata      
   if(StringUtils.isNotEmpty(metadata)){   
          try {         
        Map map = JSON.parseObject(metadata, Map.class);       
          fileSystem.setMetadata(map);     
        } catch (Exception e) {       
          e.printStackTrace();        
     }    
     }   
      //Name   
      fileSystem.setFileName(file.getOriginalFilename());  
       //Size       
  fileSystem.setFileSize(file.getSize());     
    //file type     
    fileSystem.setFileType(file.getContentType());      
   fileSystemRepository.save(fileSystem);  
       return new UploadFileResult(CommonCode.SUCCESS,fileSystem);   
    }      
//Upload the file to fdfs and return the file ID public string fdfs "upload (multipartfile file){    
     try {            
//Load the configuration of fdfs    
         initFdfsConfig();        
     //Create tracker client     
        TrackerClient trackerClient = new TrackerClient();    
         //Get trackerServer   
          TrackerServer trackerServer = trackerClient.getConnection();     
        //Get storage      
       StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);   
          //Create storage client     
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);     
        //Upload files      
       //File byte     
        byte[] bytes = file.getBytes();      
       //File original name      
       String originalFilename = file.getOriginalFilename();     
        //File extension            
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);     
        //File id
            String file1 = storageClient1.upload_file1(bytes, extName, null);
[/mw_shl_code]
[mw_shl_code=applescript,true]      
      return file1;   
      } catch (Exception e) {    
         e.printStackTrace();   
      }        
return null;  
   }   }[/mw_shl_code]
1.1.2.5 Controller 
[mw_shl_code=applescript,true]@RestController @RequestMapping("/filesystem") public class FileSystemController implements FileSystemControllerApi {     @Autowired   
  FileSystemService fileSystemService;      
@Override  
   @PostMapping("/upload")   
  public UploadFileResult upload(@RequestParam("file") MultipartFile file,                  
                  @RequestParam(value = "filetag", required = true) String  filetag,                                    @RequestParam(value = "businesskey", required = false) String  businesskey,                                    @RequestParam(value = "metedata", required = false) String  metadata) {         return fileSystemService.upload(file,filetag,businesskey,metadata);  
   } }[/mw_shl_code]

1.1.2.6 testing
Test with swagger UI or postman. The following figure shows the test interface using the swagger UI:

Posted by simple_man_11 on Fri, 29 Nov 2019 23:19:20 -0800