我没有使用任何套接字但错误" java.net.SocketException:连接重置"我确实在互联网上搜索,但找不到任何解决方案。这里有什么问题?以下是我的代码:
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;
@WebServlet("/ExecuteR")
public class ExecuteR extends HttpServlet {
private static final long serialVersionUID = 1L;
public ExecuteR() {
super();
}
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 {
// TODO Auto-generated method stub
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.BufferedReader;
import java.io.File;
import java.io.FileReader;
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{
REXP xp = null;
try{
RConnection c=Rservel.getRConnection();
c.setStringEncoding("utf8");//设置字符编码
//返回R的版本
REXP Rservesion=c.eval("R.version.string");
System.out.println(Rservesion.asString());
System.out.println("执行Excute.java路径:"+System.getProperty("user.dir"));
System.out.println("---------");
String item="隶属度";
String data=ReadData.testSaxReader(item,path);
System.out.println(data);
System.out.println("----2-----");
xp=c.eval("source(\""+path+"WEB-INF/data/"+item+".txt\")");//source是预装函数而已 rc.eval("source(\"/area.R\")");
if (xp.inherits("try-error")) System.err.println("Error: "+xp.toString());
else { System.out.println("ok"); }
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);
System.out.println("工程路径:"+System.getProperty("user.dir"));
System.out.println("用户路径:"+System.getProperty("user.home"));
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.ReadData.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(ystem.getProperty("user.dir"));
FileInputStream file = new FileInputStream(path+"WEB-INF/upload/0625.Rs");
Document doc = saxReader.read(file);
Element menu = doc.getRootElement();
// System.out.println(menu.getName());
Node person=menu.selectSingleNode("/Menu//Item[@Title='"+item+"']");//
//String nodeValue = person.getText();
// System.out.println(nodeValue);
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 nodeValue;
return person.getText().replace("\n","\r\n");
}
}
完整的错误消息:
java.net.SocketException: Connection reset
org.rosuda.REngine.Rserve.RserveException: eval failed
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:88)
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)
java.lang.NullPointerException
at com.main.service.Execute.createPlot(Execute.java:123)
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)
这个问题困扰了我好几天了,如果你能告诉我它为什么会发生以及如何解决它,我将不胜感激。