![android上傳圖片至服務(wù)器 轉(zhuǎn).docx_第1頁(yè)](http://file.renrendoc.com/FileRoot1/2020-1/9/7d20046f-34cb-44b3-8da6-a6fbefa4dea7/7d20046f-34cb-44b3-8da6-a6fbefa4dea71.gif)
![android上傳圖片至服務(wù)器 轉(zhuǎn).docx_第2頁(yè)](http://file.renrendoc.com/FileRoot1/2020-1/9/7d20046f-34cb-44b3-8da6-a6fbefa4dea7/7d20046f-34cb-44b3-8da6-a6fbefa4dea72.gif)
![android上傳圖片至服務(wù)器 轉(zhuǎn).docx_第3頁(yè)](http://file.renrendoc.com/FileRoot1/2020-1/9/7d20046f-34cb-44b3-8da6-a6fbefa4dea7/7d20046f-34cb-44b3-8da6-a6fbefa4dea73.gif)
![android上傳圖片至服務(wù)器 轉(zhuǎn).docx_第4頁(yè)](http://file.renrendoc.com/FileRoot1/2020-1/9/7d20046f-34cb-44b3-8da6-a6fbefa4dea7/7d20046f-34cb-44b3-8da6-a6fbefa4dea74.gif)
![android上傳圖片至服務(wù)器 轉(zhuǎn).docx_第5頁(yè)](http://file.renrendoc.com/FileRoot1/2020-1/9/7d20046f-34cb-44b3-8da6-a6fbefa4dea7/7d20046f-34cb-44b3-8da6-a6fbefa4dea75.gif)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
android上傳圖片至服務(wù)器 轉(zhuǎn) 本實(shí)例實(shí)現(xiàn)了android上傳手機(jī)圖片至服務(wù)器,服務(wù)器進(jìn)行保存服務(wù)器servlet代碼public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException String temp=request.getSession().getServletContext().getRealPath(/)+temp; /臨時(shí)目錄 System.out.println(temp=+temp); String loadpath=request.getSession().getServletContext().getRealPath(/)+Image; /上傳文件存放目錄 System.out.println(loadpath=+loadpath); DiskFileUpload fu = new DiskFileUpload(); fu.setSizeMax(1*1024*1024); / 設(shè)置允許用戶上傳文件大小,單位:字節(jié) fu.setSizeThreshold(4096); / 設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié) fu.setRepositoryPath(temp); / 設(shè)置一旦文件大小超過(guò)getSizeThreshold()的值時(shí)數(shù)據(jù)存放在硬盤(pán)的目錄 /開(kāi)始讀取上傳信息 int index=0; List fileItems = null; try fileItems = fu.parseRequest(request); System.out.println(fileItems=+fileItems); catch (Exception e) e.printStackTrace(); Iterator iter = fileItems.iterator(); / 依次處理每個(gè)上傳的文件 while (iter.hasNext() FileItem item = (FileItem)iter.next();/ 忽略其他不是文件域的所有表單信息 if (!item.isFormField() String name = item.getName();/獲取上傳文件名,包括路徑 name=name.substring(name.lastIndexOf()+1);/從全路徑中提取文件名 long size = item.getSize(); if(name=null|name.equals() & size=0) continue; int point = name.indexOf(.); name=(new Date().getTime()+name.substring(point,name.length()+index; index+; File fNew= new File(loadpath, name); try item.write(fNew); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); else /取出不是文件域的所有表單信息 String fieldvalue = item.getString(); /如果包含中文應(yīng)寫(xiě)為:(轉(zhuǎn)為UTF-8編碼) /String fieldvalue = new String(item.getString().getBytes(),UTF-8); String text1=11; response.sendRedirect(result.jsp?text1= + text1); 復(fù)制代碼android客戶端代碼public class PhotoUpload extends Activity private String newName = image.jpg; private String uploadFile = /sdcard/image.JPG; private String actionUrl = 1:8086/HelloWord/myForm; private TextView mText1; private TextView mText2; private Button mButton; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.photo_upload); mText1 = (TextView) findViewById(R.id.myText2); /文件路徑:n+ mText1.setText(uploadFile); mText2 = (TextView) findViewById(R.id.myText3); /上傳網(wǎng)址:n+ mText2.setText(actionUrl); /* 設(shè)置mButton的onClick事件處理 */ mButton = (Button) findViewById(R.id.myButton); mButton.setOnClickListener(new View.OnClickListener() public void onClick(View v) uploadFile(); ); /* 上傳文件至Server的方法 */ private void uploadFile() String end = rn; String twoHyphens = -; String boundary = *; try URL url =new URL(actionUrl); HttpURLConnection con=(HttpURLConnection)url.openConnection(); /* 允許Input、Output,不使用Cache */ con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); /* 設(shè)置傳送的method=POST */ con.setRequestMethod(POST); /* setRequestProperty */ con.setRequestProperty(Connection, Keep-Alive); con.setRequestProperty(Charset, UTF-8); con.setRequestProperty(Content-Type, multipart/form-data;boundary=+boundary); /* 設(shè)置DataOutputStream */ DataOutputStream ds = new DataOutputStream(con.getOutputStream(); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes(Content-Disposition: form-data; + name=file1;filename= + newName + + end); ds.writeBytes(end); /* 取得文件的FileInputStream */ FileInputStream fStream = new FileInputStream(uploadFile); /* 設(shè)置每次寫(xiě)入1024bytes */ int bufferSize = 1024; byte buffer = new bytebufferSize; int length = -1; /* 從文件讀取數(shù)據(jù)至緩沖區(qū) */ while(length = fStream.read(buffer) != -1) /* 將資料寫(xiě)入DataOutputStream中 */ ds.write(buffer, 0, length); ds.writeBytes(end); ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */ fStream.close(); ds.flush(); /* 取得Response內(nèi)容 */ InputStream is = con.getInputStream(); int ch; StringBuffer b =new StringBuffer(); while( ( ch = is.read() ) != -1 ) b.append( (char)ch ); /* 將Response顯示于Dialog */ showDialog(上傳成功+b.toString().trim(); /* 關(guān)閉DataOutputStream */ ds.close(); catch(Exception e) showDialog(上傳失敗+e); /* 顯示Dialog的method */ private void showDialog(String mess
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025合同模板中央空調(diào)銷(xiāo)售合同范本
- 北京億歐網(wǎng)盟科技有限公司-新質(zhì)生產(chǎn)力系列:2025中國(guó)消費(fèi)級(jí)AI硬件價(jià)值洞察及GEEK50榜單報(bào)告
- 2024年三年級(jí)道德與法治下冊(cè) 第四單元 多樣的交通和通信 11四通八達(dá)的交通第二課時(shí)說(shuō)課稿 新人教版
- 2024年秋七年級(jí)地理上冊(cè) 第五章 世界的發(fā)展差異 5.2《國(guó)際經(jīng)濟(jì)合作》說(shuō)課稿2 (新版)湘教版
- 9 古代科技 耀我中華(說(shuō)課稿)2024-2025學(xué)年統(tǒng)編版道德與法治五年級(jí)上冊(cè)
- 養(yǎng)殖設(shè)備銷(xiāo)售合同范例
- 2024年一年級(jí)道德與法治上冊(cè) 第16課 我有一雙明亮的眼睛說(shuō)課稿 未來(lái)版
- 9 種豆子 說(shuō)課稿-2023-2024學(xué)年科學(xué)二年級(jí)下冊(cè)冀人版
- 出售電廠鍋爐合同范例
- 人員轉(zhuǎn)公司合同范例
- 跨領(lǐng)域安檢操作標(biāo)準(zhǔn)化的現(xiàn)狀與挑戰(zhàn)
- 大模型落地應(yīng)用實(shí)踐方案
- 催收質(zhì)檢報(bào)告范文
- 2025年八省聯(lián)考內(nèi)蒙古高考生物試卷真題答案詳解(精校打印)
- 2024山東一卡通文化旅游一卡通合作協(xié)議3篇
- 人教版八年級(jí)上冊(cè)地理 2024-2025學(xué)年八年級(jí)上冊(cè)地理期中測(cè)試卷(二)(含答案)
- 2024-2025年江蘇專轉(zhuǎn)本英語(yǔ)歷年真題(含答案)
- 2024屆清華大學(xué)強(qiáng)基計(jì)劃數(shù)學(xué)學(xué)科筆試試題(附答案)
- 農(nóng)電公司績(jī)效考核管理辦法
- 斜拉橋施工技術(shù)之斜拉索圖文并茂
- GB 1886.227-2016食品安全國(guó)家標(biāo)準(zhǔn)食品添加劑嗎啉脂肪酸鹽果蠟
評(píng)論
0/150
提交評(píng)論