java上传文件到远程服务器(二)---HttpClient方式(1)
- UID
- 1066743
|
java上传文件到远程服务器(二)---HttpClient方式(1)
客户端
客户端HttpClient上传文件到远程服务器的原理是通过构造参数模仿form提交文件的http请求,把文件提交到远程服务器的接收路由中。
index.jsp的代码为:
<%@ include file="./include/header.jsp"%>
<div id="page-wrapper">
<div id="page-inner">
<div class="row">
<div class="col-md-12">
<h1 class="page-header">
HttpURLConnection <small>HttpURLConnection</small>
</h1>
</div>
</div>
<!-- /. ROW -->
<form class="form-horizontal" name="upform" action="upload" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sourceModule" class="col-sm-2 control-label">上传文件:</label>
<div class="col-sm-10">
<input type="file" name="filename"/><br/>
<input type="submit" value="提交" /><br/>
</div>
</div>
</form>
<!-- /. ROW -->
</div>
<!-- /. PAGE INNER -->
</div>
<!-- /. PAGE WRAPPER -->
<%@ include file="./include/footer.jsp"%>
<script type="text/javascript">
$(document).ready(function () {
});
</script>
</body>
</html>
需要httpclient的包,我这里使用的是httpclient4.3.3.jar和httpmime4.3。
如果使用的是maven则在pom.xml中添加:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3</version>
</dependency> |
|
|
|
|
|