//客户端代码
public static void main(String[] args) throws IOException {
DataInputStream in = null;
OutputStream out = null;
HttpURLConnection conn = null;
JSONObject resposeTxt = null;
InputStream ins = null;
ByteArrayOutputStream outStream = null;
try {
URL url = new URL("http://10.28.160.160:9080/main/uploadFile?fileName=列表.txt");
conn = (HttpURLConnection) url.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Charsert", "UTF-8");
conn.connect();
conn.setConnectTimeout(10000);
out = conn.getOutputStream();
File file = new File("H:/Users/chengtingyu/Desktop/test/list.txt");
in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
}
out.flush();
// 返回流
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
ins = conn.getInputStream();
outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int count = -1;
while ((count = ins.read(data, 0, 1024)) != -1) {
outStream.write(data, 0, count);
}
data = null;
resposeTxt = JSONObject.parseObject(new String(outStream
.toByteArray(), "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (ins != null) {
ins.close();
}
if (outStream != null) {
outStream.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
//服务端代码
public String uploadFile() throws Exception{
String fileName = request.getParameter("fileName");
String fileFullPath = "H:/Users/chengtingyu/Desktop/" + fileName;
InputStream input = null;
FileOutputStream fos = null;
try {
input = request.getInputStream();
File file = new File("H:/Users/chengtingyu/Desktop");
if(!file.exists()){
file.mkdirs();
}
fos = new FileOutputStream(fileFullPath);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer,0,1024)) != -1) {
fos.write(buffer, 0, size);
}
//响应信息 json字符串格式
Map<String,Object> responseMap = new HashMap<String,Object>();
responseMap.put("flag", true);
//生成响应的json字符串
String jsonResponse = JSONObject.toJSONString(responseMap);
sendResponse(jsonResponse);
} catch (IOException e) {
//响应信息 json字符串格式
Map<String,Object> responseMap = new HashMap<String,Object>();
responseMap.put("flag", false);
responseMap.put("errorMsg", e.getMessage());
String jsonResponse = JSONObject.toJSONString(responseMap);
sendResponse(jsonResponse);
} finally{
if(input != null){
input.close();
}
if(fos != null){
fos.close();
}
}
return null;
}
/**
* 返回响应
*
* @throws Exception
*/
private void sendResponse(String responseString) throws Exception {
response.setContentType("application/json;charset=UTF-8");
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(responseString);
pw.flush();
} finally {
IOUtils.closeQuietly(pw);
}
}
分享到:
相关推荐
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
下面小编就为大家带来一篇利用HttpUrlConnection 上传 接收文件的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本示例使用HttpUrlConnection实现上传文件,可以将手机上的文件上传到服务器 必须与my_upload工程配合运行,先启动my_upload工程
使用HttpUrlConnection实现上传文件服务器端代码,引用commons-fileupload-1.2.1.jar、commons-io-1.4.jar
Java利用HttpURLConnection发送post请求上传文件
HttpURLConnection文件下载\httpURLConnection文件下载
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
NULL 博文链接:https://dreamoftch.iteye.com/blog/1860289
HttpURLConnection实现三方接口HTTP调用调用,带有FILE文件参数
HttpUrlConnection下载文件
通过HttpURLConnection把本地目录上的资源打包远程到另一台服务器上,有通过过滤器启动Timer,有手工上传的。
这个demo是Android使用HttpURLConnection上传图片到服务器
利用HttpURLConnection进行手机号段归属地数据采集 http://zjava.org.ru/
主要介绍了Android程序开发通过HttpURLConnection上传文件到服务器的相关资料,需要的朋友可以参考下
Android HttpUrlConnection使用方法 post and get json.
Netroid框架 HttpURLConnection类 文件下载demo 主要目的是测量文件下载的速度,注意网络环境的一致性
本文通过实例代码给大家介绍了Android基于HttpUrlConnection类的文件下载功能,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
HttpURLConnection调用三方HTTP接口,带有FILE文件参数
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习