利用HttpUrlConnection 上传 接收文件的实现方法

网友投稿 362 2023-06-28


利用HttpUrlConnection 上传 接收文件的实现方法

如下所示:

//客户端代码

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&pHoFNlt;String,Object> responseMap = new HashMap();

responseMap.put("flag", true);

//生成响应的json字符串

String jsonResponse = JSONObject.toJSONString(responseMap);

sendResponse(jsonResponse);

} catch (IOException e) {

//响应信息 json字符串格式

Map responseMap = new HashMap();

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 {

http:// pw = response.getWriter();

pw.write(responseString);

pw.flush();

} finally {

IOUtils.closeQuietly(pw);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Android异常 java.lang.IllegalStateException解决方法
下一篇:javaWeb 四大域对象详细介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~