Java Path Problem final solution usage demonstration PrefaceIn the article "final solution to the Java Path Problem-locating the relative path addressing of all resources", I provide you with an assistant class classloaderutil AND ITS
Public StaticURL getextendresource (string relativepath) method. This method can accept parameters such as "../" and allow us to use relative paths to locate resources outside classpath. In this way, we can use the path relative to classpath to locate resources in all locations! In this article, I provide you with an example of using this convenient method to find relative paths in the Java EE program. In 《
Getrealpath of javaee path trapsThis article discusses the issue of resource addressing in the javaee program. If you are interested, read the article.
Demonstration of the final solution to the Java Path Problem BackgroundUse classloaderutil.
Getextendresource() This example of method addressing is a javaee program that uses springmvc framework for foreground development. The part of the uploaded file uses the Apache commons upload technology. The function of this module is to upload a workflow definition file of JBoss workflow engine jbpm to the server. Then deploy it on the server. At the same time, save the uploaded workflow definition file to
Web Application root directory/WEB-INF/jbpm/upload/Directory for reference!
Source code: ImportJava. Io. file;
ImportJava.net. Uri;
ImportJava. util. date;
ImportJavax. servlet. http. httpservletrequest;
ImportJavax. servlet. http. httpservletresponse;
ImportOrg. springframework. Web. multipart. multipartfile;
ImportOrg. springframework. Web. servlet. modelandview;
ImportCom. withub. Common. Base. basecontroller;
ImportCom. withub. Common. util. classloaderutil;
ImportCom. withub. Common. util. ideployprocessdefinition;
ImportCom. withub. WCMS. urlmap;
ImportCom. withub. WCMS. Manage. deployprocessdefinition. jbpm. Bean. fileuploadbean ;/***
@ AuthorShen Dongliang shendl_s@hotmail.com * nov27, 2006 1:31:25 pm * this class is responsible for uploading and deploying the jbpm workflow definition file * and copying uploaded files to the Web application root directory/WEB-INF/jbpm/upload/directory, for reference! **/
Public ClassUploadanddeployjbpmprocessdefinition
ExtendsBasecontroller {/*** service: deploy the locally uploaded XML business program definition file to the server database! * This bean is a singleton. This variable is not set during runtime. The set method is called only when the spring container is initialized and loaded. Pay attention to resource synchronization! */
PrivateIdeployprocessdefinition deployprocessdefinition;/*** this method directly returns to the workflow definition page for upload and deployment. This is to use. Page to control access to the upload page. *
@ ParamRequest *
@ ParamResponse *
@ Return*
@ ThrowsException */
PublicModelandview list (httpservletrequest request, httpservletresponse response)
ThrowsException {
Return NewModelandview (urlmap.
Map("Manage. deployprocessdefinition. List "));}/****
@ ParamRequest *
@ ParamResponse *
@ ParamCommand *
@ Return*
@ ThrowsException */
Public Modelandview onsubmit (httpservletrequest request, httpservletresponse response, fileuploadbean command) Throws Exception { // Let's see if there's content there Multipartfile file = command. GetFile (); If (File = Null ){ // Hmm, that's strange, the user did not upload anything Throw New Runtimeexception ( " An error occurred while uploading the file! Failed to upload file! " ); } Else { // Deploy uploaded files This . Getdeployprocessdefinition (). deployprocessdefinitiontransaction (file. getinputstream ()); File destfile = Null ; /** * With the custom method Classpath . */ String uploadpath = classloaderutil.Getextendresource( "../Jbpm/upload /" ). Tostring (); String uploadfile = uploadpath + String.Valueof( New Date (). gettime () + "_" + File. getoriginalfilename (); Destfile = New File ( New Uri (uploadfile )); File. transferto (destfile ); } // Well, Let's do nothing with the bean for now and return // Return Super. onsubmit (request, response, command, errors ); Return New Modelandview (urlmap.Map( "Manage. deployprocessdefinition. Success" )); }/***
@ ParamARGs */
Public Static VoidMain (string [] ARGs ){/****/}/***
@ ReturnThedeployprocessdefinition */
PublicIdeployprocessdefinition getdeployprocessdefinition (){
ReturnDeployprocessdefinition ;}/***
@ ParamDeployprocessdefinitionthedeployprocessdefinitiontoset */
Public VoidSetdeployprocessdefinition (ideployprocessdefinition deployprocessdefinition ){
This. Deployprocessdefinition = deployprocessdefinition ;}}
PostscriptHere, I use the self-implemented
Classloaderutil.Getextendresource ()Method to achieve
Relative Classpath .There is no addressing method provided by the servletcontext interface. Such code does not depend on the javaee environment. It depends on standard javase and can be used in any Java program! If you want to use the addressing method provided by the servletcontext interface, do not use the getrealpath ("/") method. Instead, use the getresource () method or the getresourceasstream () method for addressing. The parameter should be a relative path starting with "/", relative to the root directory of the Web application, rather than the relative path of classpath. The specific reason is as follows 《
Getrealpath of javaee path trapsThis article provides a detailed explanation.