猿问

servlet路径问题

代码一:
packagecom.main.service;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;
importjava.io.PrintWriter;
publicclassReadData{
publicstaticvoidmain(String[]args){
try{
testSaxReader("person");
System.out.println("---------");
testSaxReader("隶属度");
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidtestSaxReader(Stringitem)throwsDocumentException,FileNotFoundException{
SAXReadersaxReader=newSAXReader();
System.out.println("工程路径:"+System.getProperty("user.dir"));//问题所在
FileInputStreamfile=newFileInputStream("src/0625.xml");
Documentdoc=saxReader.read(file);
Elementmenu=doc.getRootElement();
System.out.println(menu.getName());
org.dom4j.Nodeperson=menu.selectSingleNode("/Menu//Item[@Title='"+item+"']");
StringnodeValue=person.getText();
System.out.println(nodeValue);
Filefp=newFile("./data/"+item+".txt");
PrintWriterpfp=newPrintWriter(fp);
pfp.print(nodeValue);
pfp.close();
}
}
代码一有main方法,System.out.println("工程路径:"+System.getProperty("user.dir"));打印出来的结果是工程路径:E:\workspace\outer\javaConnectR,是我项目的路径。
代码二:
packagecom.main.service;
importjava.awt.Graphics;
importjava.awt.GraphicsConfiguration;
importjava.awt.GraphicsDevice;
importjava.awt.GraphicsEnvironment;
importjava.awt.HeadlessException;
importjava.awt.Image;
importjava.awt.MediaTracker;
importjava.awt.Toolkit;
importjava.awt.Transparency;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importorg.rosuda.REngine.REXP;
importorg.rosuda.REngine.Rserve.RConnection;
importcom.main.util.Rservel;
publicclassExecuteextendsJFrame{
privatestaticfinallongserialVersionUID=1L;
staticImageimg;
publicREXPgetRobj()throwsException{
System.out.println("getRobj()");
RConnectionc=Rservel.getRConnection();
c.eval("library(RColorBrewer)");
c.eval("H<-c(7,12,28,3,41)");
c.eval("colors<-brewer.pal(5,'Set1')");
c.parseAndEval("barplot(H,rep(1,5),col=colors);dev.off()");
xp=c.parseAndEval("r=readBin('test.jpg','raw',3000*3000);unlink('test.jpg');r");
returnxp;
}
publicvoidcreatePlot(REXPxp,JFramef,Stringpath)throwsException{
try{
Imageimage=Toolkit.getDefaultToolkit().createImage(xp.asBytes());
BufferedImagebufferedImage=toBufferedImage(image);
System.out.println("工程路径:"+System.getProperty("user.dir"));
ImageIO.write(bufferedImage,"JPG",newFile(path+"/outImg/98.jpg"));//
}catch(IOExceptione){
e.printStackTrace();
}
}
publicBufferedImagetoBufferedImage(Imageimage){
if(imageinstanceofBufferedImage){
return(BufferedImage)image;
}
image=newImageIcon(image).getImage();
BufferedImagebimage=null;
GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();
try{
inttransparency=Transparency.OPAQUE;
GraphicsDevicegs=ge.getDefaultScreenDevice();
GraphicsConfigurationgc=gs.getDefaultConfiguration();
bimage=gc.createCompatibleImage(
image.getWidth(null),image.getHeight(null),transparency);
}catch(HeadlessExceptione){
}
if(bimage==null){
inttype=BufferedImage.TYPE_INT_RGB;
bimage=newBufferedImage(image.getWidth(null),image.getHeight(null),type);
}
Graphicsg=bimage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
returnbimage;
}
}
packagecom.main.web;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.rosuda.REngine.REXP;
importorg.rosuda.REngine.Rserve.RConnection;
importorg.rosuda.REngine.Rserve.RserveException;
importcom.main.service.Execute;
/**
*ServletimplementationclassExecuteR
*/
@WebServlet("/ExecuteR")
publicclassExecuteRextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
/**
*@seeHttpServlet#HttpServlet()
*/
publicExecuteR(){
super();
//TODOAuto-generatedconstructorstub
}
/**
*@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
*/
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
StringRSentences=request.getParameter("data");//这个例子必须到页面点击
//System.out.println(filePath);
Stringpath=getServletContext().getRealPath("/");
System.out.println("servlet路径:"+path);
Executewc=newExecute();
REXPxp;
try{
xp=wc.getRobj();
wc.createPlot(xp,wc,path)
}catch(Exceptione){
e.printStackTrace();
}
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doGet(request,response);
}
}
代码二:没有main方法,是一个java类,通过servlet调用,System.out.println("工程路径:"+System.getProperty("user.dir"));打印出来结果是工程路径:E:\BE\eclipse\eclipse,是我安装eclipse的路径,不是项目路径;代码Stringpath=getServletContext().getRealPath("/");打印出来结果是:servlet路径:E:\workspace\eclipse_jee_mars2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\javaConnectR\
1.请问为什么会有这样的区别??2.我希望代码二里的ImageIO.write(bufferedImage,"JPG",newFile(path+"/outImg/98.jpg"));newFile里的路径是和代码一System.getProperty("user.dir"));打印出来的路径E:\workspace\outer\javaConnectR一致,为项目路径。请问该怎么解决??
九州编程
浏览 259回答 2
2回答

慕村9548890

user.dir指向的是你启动JVM的地方,你可以看看这里代码一是你从工程路径下启动的,但是代码二你的Servlet是被容器(Tomcat等等)加载进JVM的,而此时user.dir指的是这些容器的路径,这方面你需要了解一下容器是怎样运行的。你要分清楚:开发环境、上线环境。你在E:\workspace\outer\javaConnectR写代码,这是开发环境,但是当你打包成.war时,它会被容器解压到一个目录,比如你代码二中输出的E:\workspace\...\wtpwebapps\javaConnectR\,此时它无论如何也不可能按你所想去读到开发时的路径,因为代码二实际运行的时候文件已经不在你开发的路径上了。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答