今天要作一個圖片異步上傳的功能,需求是這種java
在官網讓玩家提出問題反饋,僅僅是再發聵是需要玩家上傳圖片做爲客服審覈的證據。jquery
爲了保證官網的安全性,因而準備了一臺圖片server,專門存儲圖片。ajax
個人思路是在頁面上傳文件到圖片server,並返回圖片的地址。而後再提交表單時,將圖片url。保存入數據庫。數據庫
1. 利用 ajaxfileupload進行上傳時。是不能進行跨域操做的。跨域
2. 因此更換爲jquery.fileupload進行跨域操做,只是沒有中文文檔,使用時很是是費勁。安全
因爲第一次上傳沒有反應。僅僅有第二次的時候纔會成功。讓人很是頭疼。我對js又不是很是熟,因此準備暫且放置一下。用我比較熟的server端的開發來完畢。異步
3. 在本本服上傳文件和信息。post
而後利用httpclient 向圖片server發送請求進行保存。這個流程比較好控制。優化
如下是httpclient的代碼url
public static void main(String[] args) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); // 請求處理頁面 HttpPost httppost = new HttpPost( "http://localhost:8080/LogTest/test/upload"); // 建立待處理的文件 FileBody file = new FileBody(new File( "E:\\worktools\\resin-pro-3.1.13\\conf\\resin.conf")); // 建立待處理的表單域內容文本 StringBody descript = new StringBody("0431.la"); // 對請求的表單域進行填充 MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("fileName", file); reqEntity.addPart("descript", descript); // 設置請求 httppost.setEntity(reqEntity); // 運行 HttpResponse response = httpclient.execute(httppost); // HttpEntity resEntity = response.getEntity(); // System.out.println(response.getStatusLine()); if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { HttpEntity entity = response.getEntity(); // 顯示內容 if (entity != null) { System.out.println(EntityUtils.toString(entity)); } if (entity != null) { entity.consumeContent(); } } }
明天會將jquery.fileupload的代碼優化之後。再將該js代碼上傳。