利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法
如下所示:
//客戶端代碼
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();
// 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
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();
}
}
}
//服務(wù)端代碼
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);
}
//響應(yīng)信息 json字符串格式
Map<String,Object> responseMap = new HashMap<String,Object>();
responseMap.put("flag", true);
//生成響應(yīng)的json字符串
String jsonResponse = JSONObject.toJSONString(responseMap);
sendResponse(jsonResponse);
} catch (IOException e) {
//響應(yīng)信息 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;
}
/**
* 返回響應(yīng)
*
* @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 上傳 接收文件的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 談?wù)凧ava利用原始HttpURLConnection發(fā)送POST數(shù)據(jù)
- java后臺(tái)調(diào)用HttpURLConnection類模擬瀏覽器請(qǐng)求實(shí)例(可用于接口調(diào)用)
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- Java HttpURLConnection超時(shí)和IO異常處理
- Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
- Android中HttpURLConnection與HttpClient的使用與封裝
- Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android通過HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程
- Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
相關(guān)文章
SpringSecurity表單配置之登錄成功及頁面跳轉(zhuǎn)原理解析
這篇文章主要介紹了SpringSecurity表單配置之登錄成功及頁面跳轉(zhuǎn)原理解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12
java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案
本文介紹了java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Java底層基于二叉搜索樹實(shí)現(xiàn)集合和映射/集合Set功能詳解
這篇文章主要介紹了Java底層基于二叉搜索樹實(shí)現(xiàn)集合和映射/集合Set功能,結(jié)合實(shí)例形式分析了Java使用二叉搜索樹實(shí)現(xiàn)集合和映射相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
IDEA 去除 mybatis.xml 文件黃色警告的圖文教程
這篇文章主要介紹了IDEA 去除 mybatis.xml 文件黃色警告的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
java微信公眾號(hào)支付開發(fā)之現(xiàn)金紅包
這篇文章主要為大家詳細(xì)介紹了java微信公眾號(hào)支付開發(fā)之現(xiàn)金紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
spring security在分布式項(xiàng)目下的配置方法(案例詳解)
這篇文章主要介紹了spring security在分布式項(xiàng)目下的配置方法,本文通過一個(gè)項(xiàng)目案例給大家詳細(xì)介紹,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)
這篇文章主要介紹了Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

