servlet路径问题
代码一:
package com.main.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.PrintWriter;
public class ReadData {
public static void main(String[] args) {
try {
testSaxReader("person");
System.out.println("---------");
testSaxReader("隶属度");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testSaxReader(String item) throws DocumentException, FileNotFoundException {
SAXReader saxReader = new SAXReader();
System.out.println("工程路径:"+System.getProperty("user.dir"));//问题所在
FileInputStream file = new FileInputStream("src/0625.xml");
Document doc = saxReader.read(file);
Element menu = doc.getRootElement();
System.out.println(menu.getName());
org.dom4j.Node person=menu.selectSingleNode("/Menu//Item[@Title='"+item+"']");
String nodeValue = person.getText();
System.out.println(nodeValue);
File fp=new File("./data/"+item+".txt");
PrintWriter pfp= new PrintWriter(fp);
pfp.print(nodeValue);
pfp.close();
}
}
代码一有main方法,System.out.println("工程路径:"+System.getProperty("user.dir"));
打印出来的结果是工程路径:E:\workspace\outer\javaConnectR
,是我项目的路径。
代码二:
package com.main.service;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;
import com.main.util.Rservel;
public class Execute extends JFrame{
private static final long serialVersionUID=1L;
static Image img;
public REXP getRobj() throws Exception{
System.out.println("getRobj()");
RConnection c=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");
return xp;
}
public void createPlot(REXP xp, JFrame f,String path)throws Exception{
try {
Image image=Toolkit.getDefaultToolkit().createImage(xp.asBytes());
BufferedImage bufferedImage=toBufferedImage(image);
System.out.println("工程路径:"+System.getProperty("user.dir"));
ImageIO.write(bufferedImage, "JPG", new File(path+"/outImg/98.jpg"));//
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(
image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
package com.main.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import com.main.service.Execute;
/**
* Servlet implementation class ExecuteR
*/
@WebServlet("/ExecuteR")
public class ExecuteR extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ExecuteR() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String RSentences=request.getParameter("data");//这个例子必须到页面点击
//System.out.println(filePath);
String path=getServletContext().getRealPath("/");
System.out.println("servlet路径:"+path);
Execute wc=new Execute();
REXP xp;
try {
xp = wc.getRobj();
wc.createPlot(xp,wc,path)
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
代码二:没有main方法,是一个java类,通过servlet调用,System.out.println("工程路径:"+System.getProperty("user.dir"));
打印出来结果是工程路径:E:\BE\eclipse\eclipse
,是我安装eclipse的路径,不是项目路径;代码String path=getServletContext().getRealPath("/");
打印出来结果是:servlet路径:E:\workspace\eclipse_jee_mars2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\javaConnectR\
1.请问为什么会有这样的区别??
2.我希望代码二里的ImageIO.write(bufferedImage, "JPG", new File(path+"/outImg/98.jpg"));
new File里的路径是和代码一System.getProperty("user.dir"));
打印出来的路径E:\workspace\outer\javaConnectR
一致,为项目路径。请问该怎么解决??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
user.dir
指向的是你启动JVM
的地方,你可以看看这里代码一是你从工程路径下启动的,但是代码二你的
Servlet
是被容器 (Tomcat
等等)加载进JVM
的,而此时user.dir
指的是这些容器的路径,这方面你需要了解一下容器是怎样运行的。你在
E:\workspace\outer\javaConnectR
写代码,这是开发环境,但是当你打包成.war
时,它会被容器解压到一个目录,比如你代码二中输出的E:\workspace\...\wtpwebapps\javaConnectR\
,此时它无论如何也不可能按你所想去读到开发时的路径,因为代码二实际运行的时候文件已经不在你开发的路径上了。我就用通俗的话来说一下吧:
System.out.println("工程路径:"+System.getProperty("user.dir"));
这段代码由JVM执行,因此输出的是类路径,getServletContext().getRealPath("/");
是由Tomcat执行,在Eclipse下配置Tomcat,Tomcat会将代码默认输出到另外的文件夹中(当然了,该路径是可以配置的)。它俩输出的结果不同主要是因为执行的主体不一致,JVM和Tocmat
devProjectPath=http://localhost:8080/xxx
,devProjectPath=http://localhost:8080/xxx
,productionProjectPath=http://www.xxxx.com
水平有限,如果有错的地方请指出