Tomcat经理脚本部署大型战争无法正常工作

时间:2015-01-21 10:42:14

标签: java tomcat deployment apache-httpclient-4.x tomcat8

我正在尝试使用Manager-Script servlet上传WAR,在tomcat 8.0.12 中自动部署WAR。

对此网址执行PUT:

http://tomcaserver:8080/manager/text/deploy?path=/myApp&update=true

我的WAR大小约为80mb,我认为这就是问题所在。

我使用小型WAR(大约500kb)进行了测试,上传时没有问题。

我已经尝试在/webapps/manager/WEB-INF/web.xml中指定multipart-config,插入:

<servlet>
  <servlet-name>Manager</servlet-name>
  <servlet-class>org.apache.catalina.manager.ManagerServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>2</param-value>
  </init-param>
  <multipart-config>
    <!-- 200MB max -->
    <max-file-size>209715200</max-file-size>
    <max-request-size>209715200</max-request-size>
    <file-size-threshold>0</file-size-threshold>
  </multipart-config>
</servlet>

在此之后,问题仍然存在。

我正在使用Apache HTTPClient上传,这是个例外:

Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
    at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:123)
    at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:135)
    at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:164)
    at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115)
    at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:120)
    at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:150)
    at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:173)
    at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:97)
    at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:155)
    at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:149)
    at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:236)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at br.com.jjw.atualizador.core.Atualizador.deploy(Atualizador.java:175)

这是我的代码:

    String url = "http://tomcatserver:8080/manager/text/deploy?path=/organize&update=true";

    HttpPut req = new HttpPut(url);
    MultipartEntityBuilder meb = MultipartEntityBuilder.create();
    // upload the big war (80mb)
    meb.addBinaryBody("attachment", war, ContentType.APPLICATION_OCTET_STREAM, "myApp.war");
    req.setEntity(meb.build());

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("tomcat", "s3cret"));

    CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    CloseableHttpResponse resp = client.execute(req); <<<---- EXCEPTION!

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题在于您构建文件上传的奇怪方式。我不知道“附件”会做什么,但HTTP PUT是正确的。

试试这个:

String url = "http://tomcatserver:8080/manager/text/deploy?path=/organize&update=true";

HttpPut req = new HttpPut(url);
File war = new File("myApp.war");
req.setEntity(new FileEntity(war, ContentType.APPLICATION_OCTET_STREAM));

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("tomcat", "s3cret"));

CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
CloseableHttpResponse resp = client.execute(req);