碰巧的機(jī)會,把Android上傳圖片到Tomcat端的代碼又學(xué)習(xí)了一遍,這里總結(jié)一下,分享經(jīng)驗。
Android端
1 Android中AsyncTask的使用,這個東西是比較重要的,要學(xué)會用。下面鏈接寫得好。
https://blog.csdn.net/iispring/article/details/50639090
2 從相冊中選擇圖片
//1 獲取activity服務(wù)
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//2 獲取數(shù)據(jù)位置
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// 獲取游標(biāo)
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
// 3 根據(jù)路徑獲取,轉(zhuǎn)化為File
file = new File(Environment.getExternalStorageDirectory(), filename +".png");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(stream.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
最后就是通過網(wǎng)絡(luò)框架,將數(shù)據(jù)作為參數(shù)上傳到服務(wù)器,即可。
這些操作都大同小異。
服務(wù)器端
服務(wù)端是通過Java的Servlet來實現(xiàn)的,里面使用了一些框架,總之效果不錯。而感覺前臺參數(shù)來獲取文件的代碼,主要是對FileItem進(jìn)行遍歷,找到需要的文件。
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
try
{
request.setCharacterEncoding("UTF-8"); // 設(shè)置處理請求參數(shù)的編碼格式
response.setContentType("text/html;charset=UTF-8"); // 設(shè)置Content-Type字段值
PrintWriter out = response.getWriter();
// 下面的代碼開始使用Commons-UploadFile組件處理上傳的文件數(shù)據(jù)
FileItemFactory factory = new DiskFileItemFactory(); // 建立FileItemFactory對象
ServletFileUpload upload = new ServletFileUpload(factory);
// 分析請求,并得到上傳文件的FileItem對象
List<FileItem> items = upload.parseRequest(request);
// 從web.xml文件中的參數(shù)中得到上傳文件的路徑
String uploadPath = "d:\\upload\\";
File file = new File(uploadPath);
if (!file.exists())
{
file.mkdir();
}
String filename = ""; // 上傳文件保存到服務(wù)器的文件名
InputStream is = null; // 當(dāng)前上傳文件的InputStream對象
// 循環(huán)處理上傳文件
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(""))
{
// 從客戶端發(fā)送過來的上傳文件路徑中截取文件名
filename = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
is = item.getInputStream(); // 得到上傳文件的InputStream對象
}
}
// 將路徑和上傳文件名組合成完整的服務(wù)端路徑
filename = uploadPath + filename;
// 如果服務(wù)器已經(jīng)存在和上傳文件同名的文件,則輸出提示信息
if (new File(filename).exists())
{
new File(filename).delete();
}
// 開始上傳文件
if (!filename.equals(""))
{
// 用FileOutputStream打開服務(wù)端的上傳文件
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[8192]; // 每次讀8K字節(jié)
int count = 0;
// 開始讀取上傳文件的字節(jié),并將其輸出到服務(wù)端的上傳文件輸出流中
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count); // 向服務(wù)端文件寫入字節(jié)流
}
fos.close(); // 關(guān)閉FileOutputStream對象
is.close(); // InputStream對象
out.println("文件上傳成功!");
}
}
catch (Exception e)
{
}
}
剩下也就沒什么了,找個時間要好好總結(jié)一下之前做的服務(wù)端的知識。