Spring's excellent tool class inventory, part 1th: File resource Operations and Web-related

Source: Internet
Author: User
Tags file system resource root directory

Not only does Spring provide a full-featured application development framework, but it also has a number of tool classes that you can use directly when you write a program, not only in spring applications but also in other applications, most of which can be detached from spring The frame is used. Knowing what handy tool classes are available in Spring and using them appropriately when you write them will help improve development efficiency and enhance code quality.

In this two-part article, we'll pick out the handy tools classes from a number of Spring tool classes. Part 1th will describe the tool classes related to file resource operations and Web. The special character escape and method entry instrumentation tool classes are introduced in part 2nd.

File Resource Operations

The operation of a file resource is a common feature in an application, such as uploading a file to a specific directory, loading a configuration file from a specified address, and so on. We typically do these with JDK's I/O processing classes, but for ordinary applications, the JDK's operations classes provide too much of a way to use them directly for file operations, which are complex and prone to errors. Compared to the JDK's file,spring Resource interface (the description interface for resource concepts), the abstraction level is higher and broader, and Spring offers a number of easy-to-use resource manipulation tools that greatly reduce the complexity of resource operations and are more universal. These tool classes do not depend on the Spring container, which means that you can use them in your program like normal classes.

Loading file Resources

Spring defines a Org.springframework.core.io.Resource interface, Resource interfaces are defined to unify different types of resources, and Spring provides implementation classes for several Resource interfaces. These implementation classes can easily load different types of underlying resources and provide an action method for obtaining file names, URL addresses, and resource content.

accessing File Resources

Assuming that a file is located under the classpath of a WEB application, you can access the file resource in the following ways:

Access through Filesystemresource in the way of absolute path of file system;

Access via Classpathresource in the form of classpath;

Accessed through Servletcontextresource in a way that is relative to the Web application root directory.

The Spring Resource implementation class provides a more flexible way of doing things than accessing file resources through the JDK's file class, and you can choose the appropriate Resource implementation class to access resources depending on the situation. Below, we access the same file resource through Filesystemresource and Classpathresource respectively:

Listing 1. Filesourceexample

package com.baobaotao.io;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class FileSourceExample {
  public static void main(String[] args) {
    try {
      String filePath =
      "D:/masterSpring/chapter23/webapp/WEB-INF/classes/conf/file1.txt";
      // ① 使用系统文件路径方式加载文件
      Resource res1 = new FileSystemResource(filePath);
      // ② 使用类路径方式加载文件
      Resource res2 = new ClassPathResource("conf/file1.txt");
      InputStream ins1 = res1.getInputStream();
      InputStream ins2 = res2.getInputStream();
      System.out.println("res1:"+res1.getFilename());
      System.out.println("res2:"+res2.getFilename());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.