關于httpClient上傳文件
最近要做客戶端上傳圖片和參數,服務器整合了SSH . 廢話不多說,上代碼
客戶端:
import java.io.File; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; public class Test(){ //photoFileName public void upload() throws Exception{ String url = "http://localhost:8080/scan/mperson_upLoadImage.action"; HashMap<String, Object>params =new HashMap<String, Object>(); params.put("id", 3); String imagepath = "C:/Users/zsq/Desktop/abc.png"; MultipartEntity mpEntity = new MultipartEntity(); if (params != null && !params.isEmpty()) { for (Map.Entry<String, Object> entry : params.entrySet()) { //參數名 StringBody par = new StringBody(entry.getValue().toString()); mpEntity.addPart(entry.getKey(), par); } } // 圖片 if (!imagepath.equals("")) { FileBody file = new FileBody(new File(imagepath)); mpEntity.addPart("photo", file); } // 使用HttpPost對象設置發送的URL路徑 HttpPost post = new HttpPost(url); post.setHeader("Cookie", "JSESSIONID=" + JSESSIONID); // 發送請求體 post.setEntity(mpEntity); // 創建一個瀏覽器對象,以把POST對象向服務器發送,并返回響應消息 DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 封裝了服務器端返回的數據 HttpEntity responseEntity = response.getEntity(); // 將服務器返回的輸入流 解析為 字符串 // EntityUtils.toString(responseEntity) //對服務器返回的session進行記錄 CookieStore mCookieStore = ((DefaultHttpClient) client).getCookieStore(); List<Cookie> cookies = mCookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { //如果cookies頭和"JSESSIONID" 就記錄sessionID if("JSESSIONID".equals(cookies.get(i).getName())){ JSESSIONID = cookies.get(i).getValue(); break; } } InputStream is = responseEntity.getContent(); String result = StreamUtils.read(is); System.out.println("上傳的結果是 : "+ result); } } }javaWeb端
public void upLoadImage () throws Exception{ System.out.println("有到upLoadImage這里來?"); String realPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/person_photo"); System.out.println(realPath); System.out.println("uploadFileName : "+photoFileName); UUID uuid = UUID.randomUUID(); int id = model.getId(); System.out.println("得到的ID: "+model.getId()); //System.out.println("photoContentType"+v); String fileLastName = photoFileName.substring(photoFileName.lastIndexOf(".")); //存放的是UUID的名字 String uuidName = uuid.toString()+fileLastName; PrintWriter writer = getWriter(); if(photo!=null){ File saveFile = new File(new File(realPath),uuidName); FileUtils.copyFile(photo, saveFile); writer.print("{scuess:true}"); }else{ writer.print("{scuess:false,error:'photo_null'}"); } }uploadFileName // 是strust2 自動把上傳文件的名字封裝到這個uploadFileName
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!