lzzscl,这个SB
2018-09-15 07:54
采纳率: 81.8%
浏览 1.5k
已采纳

跨服务器上传数据一直出现UnknownHostException: loaclhost

萌新学习,在跨服务器上传数据时,一直报着个错,求大神们指教

 com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: loaclhost
Caused by: java.net.UnknownHostException: loaclhost
2018-9-15 15:37:48 org.apache.catalina.core.StandardWrapperValve invoke
**严重: Servlet.service() for servlet [springMVC] in context with path [/ssm] threw exception [Request processing failed; nested exception is com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: loaclhost] with root cause
java.net.UnknownHostException: loaclhost**
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:852)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:718)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:896)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler$1$1.getOutputStream(URLConnectionClientHandler.java:238)
    at com.sun.jersey.api.client.CommittingOutputStream.commitStream(CommittingOutputStream.java:117)
    at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:83)
    at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:95)
    at com.sun.jersey.core.impl.provider.entity.ByteArrayProvider.writeTo(ByteArrayProvider.java:59)
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
    at com.sun.jersey.api.client.Client.handle(Client.java:652)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
    at com.sun.jersey.api.client.WebResource.put(WebResource.java:223)
    at com.yihengjie.controller.UploadController.uploadPic(UploadController.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

存数据的服务器配置:

 <Server port="8003" shutdown="SHUTDOWN">
 <Connector port="8004" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <Connector port="8006" protocol="AJP/1.3" redirectPort="8443" />

执行存储的代码:

 public class SSMUtils {

    public static final String pic_HOST="http://loaclhost:8004/ssm_image";

}
 @Controller
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping("/uploadPic")
    public void uploadPic(HttpServletRequest request, String fileName,PrintWriter out)
    {
//      把request转换成多部件请求
        MultipartHttpServletRequest mh = (MultipartHttpServletRequest) request;
        CommonsMultipartFile cm = (CommonsMultipartFile) mh.getFile(fileName);      
        byte[] fbytes = cm.getBytes();
        //准备图片名称算法
        String newFileName="";
        SimpleDateFormat adf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        newFileName = adf.format(new Date());
        Random rd = new Random();
        for(int i=0;i<3;i++){
            newFileName = newFileName+rd.nextInt(10);
        }
        //获取文件名
        String sur = cm.getOriginalFilename();      
        //截取文件的扩展名
        String suffix = sur.substring(sur.lastIndexOf("."));
        //创建jersy服务器
        Client client = Client.create();

        WebResource resource = client.resource(SSMUtils.pic_HOST+"/upload/"+newFileName+suffix);

        resource.put(String.class, fbytes);

        //准备回显图片路径地址
        String fullPath=SSMUtils.pic_HOST+"/upload/"+newFileName+suffix;
        String relativePath="/upload/"+newFileName+suffix;

        String result = "{\"fullPath\":\""+fullPath+"\",\"relativePath\":\""+relativePath+"\"}";

        out.print(result);

    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • -syso- 2018-09-18 01:24
    已采纳

    你试试把localhost改成127.0.0.1

    点赞 打赏 评论
  • devmiao 2018-09-15 11:52
    点赞 打赏 评论

相关推荐 更多相似问题