我的代码如下有三个:
1.servlet类
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 org.dom4j.Node;
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();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String RSentences=request.getParameter("data");//这个例子必须到页面点击
String path=getServletContext().getRealPath("/");
System.out.println("servlet路径:"+path);
Execute wc=new Execute();
REXP xp;
try {
xp = wc.getRobj(path);//获得R对象
wc.createPlot(xp,wc,path);//
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.Execute.java
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 org.dom4j.Node;
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;
import com.main.service.ReadData;
public class Execute extends JFrame{
private static final long serialVersionUID=1L;
static Image img;
public REXP getRobj(String path) throws Exception{
System.out.println("getRobj()");
REXP xp = null;
try{
RConnection c=Rservel.getRConnection();
System.out.println("c=Rservel.getRConnection()......"+c);
c.setStringEncoding("utf8");//设置字符编码
//返回R的版本
REXP Rservesion=c.eval("R.version.string");
System.out.println("执行Excute.java路径:"+System.getProperty("user.dir"));
System.out.println("---------");
String item="隶属度";
String data=ReadData.testSaxReader(item,path);
System.out.println(data);
xp=c.eval("source('"+path+"WEB-INF/data/"+item+".txt')");
System.out.println(xp);
System.out.println(Rservesion.asString());
xp=c.parseAndEval("jpeg('test.jpg',quality=90)");
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");
c.close();
}catch(Exception e){
System.out.println(e);
}
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);
ImageIO.write(bufferedImage, "JPG", new File(path+"/images/routput/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;
}
}
3.ReaderData.java
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;
import org.dom4j.Node;
public class ReadData {
}
public static String testSaxReader(String item,String path) throws DocumentException, FileNotFoundException {
SAXReader saxReader = new SAXReader();
System.out.println("工程路径:"+System.getProperty("user.dir"));
FileInputStream file = new FileInputStream(path+"WEB-INF/upload/0625.Rs");
Document doc = saxReader.read(file);
Element menu = doc.getRootElement(); //获取根元素
Node person=menu.selectSingleNode("/Menu//Item[@Title='"+item+"']");//
File fp=new File(path+"WEB-INF/data/"+item+".txt");
PrintWriter pfp= new PrintWriter(fp);
pfp.print(person.getText().replace("\n","\r\n"));
pfp.close();
return person.getText().replace("\n","\r\n");
}
}
代码运行后有以下错误:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:213)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:180)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:250)
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:231)
at com.main.service.Execute.getRobj(Execute.java:83)
at com.main.web.ExecuteR.doGet(ExecuteR.java:45)
at com.main.web.ExecuteR.doPost(ExecuteR.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
org.rosuda.REngine.Rserve.RserveException: eval failed
java.lang.NullPointerException
at com.main.service.Execute.createPlot(Execute.java:106)
at com.main.web.ExecuteR.doGet(ExecuteR.java:46)
at com.main.web.ExecuteR.doPost(ExecuteR.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
请问我是哪方面的问题没处理好?该怎么处理?
java.lang.NullPointerException
应该是空指针异常引发的socke错误,检查赋值操作为NULL的情况