標籤:android style blog http io color ar os 使用
最近在做一個手機app聯合系統管理做的應用程式,管理程式管理資料的發布和增刪改查,手機app負責顯示和操作商務邏輯這麼一個功能。
剛開始路走的都很順,但是走到通過Android用戶端上傳圖片到Asp.Net 伺服器的時候出現很大的問題,圖片是上傳了,就是顯示不出來,用相片檢視器查看的時候顯示:‘沒有預覽’,用畫圖軟體開啟的時候顯示‘無效的位元影像檔案或不支援檔案的格式’!!!
大家想,肯定你的代碼寫得有問題,好吧,你們看看My Code是什麼問題吧:
Stream sr = context.Request.InputStream; byte[] buffer = new byte[4096]; int bytesRead = 0; //將當前資料流寫入伺服器端檔案夾ClientBin下 const string savePath = "/RiskNoticeImg"; //靶心圖表片路徑 string dirPath = context.Server.MapPath(savePath); string path = dirPath + @"\" + picName; using (FileStream fs = File.Create(filePath, 4096)) { while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0) { //向檔案中寫資訊 fs.Write(buffer, 0, bytesRead); } }
看看,你們覺得有什麼問題嗎?我是看不出來,搞了兩天,人都快崩潰了,然後漫無目的的在網上找答案,有的說要Base64編碼才行,但是還是不行,也不是說不行,是我沒有試,最噁心的是這篇文章:點擊這裡查看,為什麼這麼說呢,因為明明跟我是一樣的問題,明明他也解決了,但是就是不告訴你怎麼解決的,無奈之下求助專案經理,經過他電腦般的大腦高速運轉就解決問題了,接下來就是見證奇蹟的時刻:
context.Request.Files[0].SaveAs(filePath);
是的,就這一行代碼就搞定了,如果你測試這行代碼搞不定,那你可以根據它的Files.Count來迴圈輸出它的檔案像這樣:context.Request.Files[i].SaveAs(filePath);這樣就解決上傳圖片顯示不了的問題了,希望對有些人有所協助吧^_^,
最後想講下,他是通過這個檔案得到的靈感:
package main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class hello extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
try
{
request.setCharacterEncoding("UTF-8"); // 設定處理請求參數的編碼格式
response.setContentType("text/html;charset=UTF-8"); // 設定Content-Type欄位值
PrintWriter out = response.getWriter();
// 下面的代碼開始使用Commons-UploadFile組件處理上傳的檔案資料
FileItemFactory factory = new DiskFileItemFactory(); // 建立FileItemFactory對象
ServletFileUpload upload = new ServletFileUpload(factory);
// 分析請求,並得到上傳檔案的FileItem對象
List<FileItem> items = upload.parseRequest(request);
// 從web.xml檔案中的參數中得到上傳檔案的路徑
String uploadPath = "d:\\upload\\";
File file = new File(uploadPath);
if (!file.exists())
{
file.mkdir();
}
String filename = ""; // 上傳檔案儲存到伺服器的檔案名稱
InputStream is = null; // 當前上傳檔案的InputStream對象
// 迴圈處理上傳檔案
for (FileItem item : items)
{
// 處理普通的表單域
if (item.isFormField())
{
if (item.getFieldName().equals("filename"))
{
// 如果新檔案不為空白,將其儲存在filename中
if (!item.getString().equals(""))
filename = item.getString("UTF-8");
}
}
// 處理上傳檔案
else if (item.getName() != null && !item.getName().equals(""))
{
// 從用戶端發送過來的上傳檔案路徑中截取檔案名稱
filename = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
is = item.getInputStream(); // 得到上傳檔案的InputStream對象
}
}
// 將路徑和上傳檔案名稱組合成完整的服務端路徑
filename = uploadPath + filename;
// 如果伺服器已經存在和上傳檔案同名的檔案,則輸出提示資訊
if (new File(filename).exists())
{
new File(filename).delete();
}
// 開始上傳檔案
if (!filename.equals(""))
{
// 用FileOutputStream開啟服務端的上傳檔案
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[8192]; // 每次讀8K位元組
int count = 0;
// 開始讀取上傳檔案的位元組,並將其輸出到服務端的上傳檔案輸出資料流中
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count); // 向服務端檔案寫入位元組流
}
fos.close(); // 關閉FileOutputStream對象
is.close(); // InputStream對象
System.out.println("檔案上傳成功!!!");
out.println("檔案上傳成功!");
}
}
catch (Exception e)
{
System.out.println("出現異常");
e.printStackTrace();
}
}
}
Android 上傳圖片到 Asp.Net 伺服器的問題