The Spring framework comes with a rich toolset that simplifies a lot of work as we develop :
1.Resource access to File resources:
Specifically:
Resourceutils.getfile (URL); Filesystemresource (); Classpathresource (); Servletcontextresource (Application,url);
2. File Operation Filecopyutils
Specifically:
Filecopyutils.copy (Resource.getfile,new file (Resource.getfile (), getParent () + ' target file name '));
3. property file Operation Propertiesloaderutils
Specifically:
Propertiesloaderutils.loadallproperties ("attribute file name"); --Based on class path
4.EncodedResource (Resource object, "Utf-8″") encoded resource (special);
5.WebApplicationContextUtils
6.StringEscapeutils encoding and decoding
File Resource Operations
- Access via Filesystemresource in the absolute path of the file system;
- Access by Classpathresource in a classpath manner;
- Access is done through Servletcontextresource in a way that is relative to the Web application's root directory.
Importjava.io.IOException;ImportJava.io.InputStream;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.FileSystemResource;ImportOrg.springframework.core.io.Resource; Public classFilesourceexample { Public Static voidMain (string[] args) {Try{String FilePath= "D:/masterspring/chapter23/webapp/web-inf/classes/conf/file1.txt"; //① loading files using the System file path methodResource res1 =NewFilesystemresource (FilePath); //② loading files with classpath modeResource Res2 =NewClasspathresource ("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 (); } }}
Once the resource has been obtained, you can access the file's data and other information through multiple methods defined by the Resource interface
GetFileName () Gets the file name,
GetFile () Gets the File object corresponding to the resource,
getInputStream () gets the input stream of the file directly.
Createrelative (String relativepath) creates a new resource on the relative address of the resource.
In a web app, you can also access file resources by Servletcontextresource in a way that is relative to the Web app's root directory
Spring provides a resourceutils tool class that supports the address prefixes of "classpath:" and "File:", which can load file resources from a specified address.
File clsfile = Resourceutils.getfile ("Classpath:conf/file1.txt"); = "File:d:/masterspring/chapter23/src/conf/file1.txt"= Resourceutils.getfile (Httpfilepath);
file Operations
After loading the file resources with the implementation classes of various Resource interfaces, it is often necessary to read, copy, and dump the file resources.
Filecopyutils
It provides a number of one-step, static operations that can copy the contents of a file to a target byte[], String, or even an output stream or output file.
ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.OutputStream;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;Importorg.springframework.util.FileCopyUtils; Public classFilecopyutilsexample { Public Static voidMain (string[] args)throwsThrowable {Resource res=NewClasspathresource ("Conf/file1.txt"); //① Copy the contents of the file to a byte[] byte[] FileData =Filecopyutils.copytobytearray (Res.getfile ()); //② Copying the contents of a file into a StringString filestr = filecopyutils.copytostring (NewFileReader (Res.getfile ())); //③ Copying the contents of a file to another destination filefilecopyutils.copy (Res.getfile (),NewFile (Res.getfile (). GetParent () + "/file2.txt")); //④ Copying the contents of a file into an output streamOutputStream OS =NewBytearrayoutputstream (); Filecopyutils.copy (Res.getinputstream (), OS); }}Static voidCopybyte[] in, File out) willbyte[] Copy to a fileStatic voidCopybyte[] in, outputstream out) willbyte[] Copy to an output streamStatic intCopy (file, file out) copies files to another fileStatic intCopy (InputStream in, outputstream out) copies the input stream to the output streamStatic intCopy (reader in, writer out) copies the content read by reader to Writer pointing to the target outputStatic voidCopy (string in, writer out) copies the string to a target in which writer points a property file operation Spring provides a propertiesloaderutils that allows you to load directly from a file address based on a classpath Sexual Resources PackageCom.baobaotao.io;Importjava.util.Properties;Importorg.springframework.core.io.support.PropertiesLoaderUtils; Public classPropertiesloaderutilsexample { Public Static voidMain (string[] args)throwsThrowable {//①jdbc.properties is a file located under the ClasspathProperties props = propertiesloaderutils.loadallproperties ("Jdbc.properties"); System.out.println (Props.getproperty ("Jdbc.driverclassname")); }}
Specially coded Resources
When you use the Resource implementation class to load a file resource, it defaults to the encoding format of the operating system.
If the file resource takes a special encoding format (such as UTF-8), it is necessary to specify the encoding format in advance by Encodedresource when reading the resource content, otherwise the problem of garbled Chinese will be generated.
ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.core.io.support.EncodedResource;Importorg.springframework.util.FileCopyUtils; Public classEncodedresourceexample { Public Static voidMain (string[] args)throwsThrowable {Resource res=NewClasspathresource ("Conf/file1.txt"); //① the encoding format for the specified file resource (UTF-8)Encodedresource encres =NewEncodedresource (res, "UTF-8"); //② so that the contents of the file can be read correctly without garbledString content =filecopyutils.copytostring (Encres.getreader ()); SYSTEM.OUT.PRINTLN (content); }}
Access the Spring container, get the beans in the container, use the Webapplicationcontextutils tool class
ServletContext ServletContext == webapplicationcontextutils. Getwebapplicationcontext (ServletContext);
Chinese garbled filter
<Filter><Filter-name>Encodingfilter</Filter-name><Filter-class>org.springframework.web.filter.characterencodingfilter①spring Edit Filter</Filter-class><Init-param>② Encoding Method<Param-name>Encoding</Param-name> <Param-value>UTF-8</Param-value></Init-param><Init-param>③ forcing the encoding conversion<Param-name>Forceencoding</Param-name> <Param-value>True</Param-value></Init-param></Filter><filter-mapping>Match URL for ② filter<Filter-name>Encodingfilter</Filter-name> <Url-pattern>*.html</Url-pattern></filter-mapping>
special character Escapes
Web developers are most often confronted with special character types that need to be escaped: HTML special characters; JavaScript special characters; escape from HTML special character &:& ":" < : < ; > :> JavaScript special character escapes ':/' ":/"//:// Go paper for page:/F Wrap:/ N newline:/ t carriage return:/R fallback:/b
Spring Common tool Classes