利用HttpUrlConnection 上传 接收文件方法
来源:互联网 发布:知乎专栏文章怎么收藏 编辑:程序博客网 时间:2024/05/29 16:46
/客户端代码
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);
}
}
- 利用HttpUrlConnection 上传 接收文件方法
- 利用HttpUrlConnection 上传 接收文件方法
- 利用HttpUrlConnection 上传 接收文件的实现方法
- 文件上传接收方法
- 利用HttpURLConnection发送post请求上传文件
- 利用HttpURLConnection发送post请求上传文件
- 利用HttpURLConnection发送post请求上传文件
- HTTP文件上传请求格式详解,利用HttpURLConnection上传文件
- 利用HttpURLConnection发送post请求上传多个文件
- Java利用HttpURLConnection发送post请求上传文件
- 利用HttpURLConnection发送post请求上传多个文件
- 利用HttpURLConnection发送post请求上传多个文件
- Java利用HttpURLConnection发送post请求上传文件
- 利用HttpURLConnection发送post请求上传多个文件
- 利用HttpURLConnection发送post请求上传多个文件
- 利用HttpURLConnection发送post请求上传多个文件
- 使用HttpURLConnection上传文件
- HttpURLConnection POST 上传文件
- Spring Boot CLI 安装
- caffe学习笔记(13):python cv2.imread()读取图片和matlab读取图像的区别
- Java String深入理解
- 大四最后的一个多月,我想开始写点什么,记录一下自己能在这条路走多远
- JavaScript通过Ajax向后台传输
- 利用HttpUrlConnection 上传 接收文件方法
- sprintf与printf的%n作用
- mySQL中replace的用法
- Windows下适用于PHP 7.1的memcache扩展工具编译
- Python socket编程学习(二)socket客户端
- 在使用jQuery插件时应避免重复引入jquery.js文件
- android 透明状态栏(设置纯色背景)
- vxWorks系统shell命令记录(持续)
- 警告: Could not load driverClass com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdb