很久没写点什么了,本意是边学习边写点,一来为了自己收藏二来为了共享,但是,最近有点懒,到现在才写。自己最近做了一个小项目,设计到camera拍照以及照片上传服务器等功能。现在把本地照片上传服务器的部分代码写上,跟大家分享!所用到的服务器是tomcat。
下面是服务器java代码:
1 package net.blogjava.mobile; 2 3 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.PrintWriter; 9 import java.util.List; 10 11 import javax.servlet.ServletException; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 import org.apache.commons.fileupload.FileItem; 17 import org.apache.commons.fileupload.FileItemFactory; 18 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 19 import org.apache.commons.fileupload.servlet.ServletFileUpload; 20 21 public class UploadServlet extends HttpServlet 22 { 23 protected void service(HttpServletRequest request, 24 HttpServletResponse response) throws ServletException, IOException 25 { 26 try 27 { 28 request.setCharacterEncoding("UTF-8"); // 设置处理请求参数的编码格式 29 response.setContentType("text/html;charset=UTF-8"); // 设置Content-Type字段值 30 PrintWriter out = response.getWriter(); 31 32 // 下面的代码开始使用Commons-UploadFile组件处理上传的文件数据 33 FileItemFactory factory = new DiskFileItemFactory(); // 建立FileItemFactory对象 34 ServletFileUpload upload = new ServletFileUpload(factory); 35 // 分析请求,并得到上传文件的FileItem对象 36 List<FileItem> items = upload.parseRequest(request); 37 // 从web.xml文件中的参数中得到上传文件的路径 38 String uploadPath = "d:\\upload\\"; 39 File file = new File(uploadPath); 40 if (!file.exists()) 41 { 42 file.mkdir(); 43 } 44 String filename = ""; // 上传文件保存到服务器的文件名 45 InputStream is = null; // 当前上传文件的InputStream对象 46 // 循环处理上传文件 47 for (FileItem item : items) 48 { 49 // 处理普通的表单域 50 if (item.isFormField()) 51 { 52 if (item.getFieldName().equals("filename")) 53 { 54 // 如果新文件不为空,将其保存在filename中 55 if (!item.getString().equals("")) 56 filename = item.getString("UTF-8"); 57 } 58 } 59 // 处理上传文件 60 else if (item.getName() != null && !item.getName().equals("")) 61 { 62 // 从客户端发送过来的上传文件路径中截取文件名 63 filename = item.getName().substring( 64 item.getName().lastIndexOf("\\") + 1); 65 is = item.getInputStream(); // 得到上传文件的InputStream对象 66 } 67 } 68 // 将路径和上传文件名组合成完整的服务端路径 69 filename = uploadPath + filename; 70 // 如果服务器已经存在和上传文件同名的文件,则输出提示信息 71 if (new File(filename).exists()) 72 { 73 new File(filename).delete(); 74 } 75 // 开始上传文件 76 if (!filename.equals("")) 77 { 78 // 用FileOutputStream打开服务端的上传文件 79 FileOutputStream fos = new FileOutputStream(filename); 80 byte[] buffer = new byte[8192]; // 每次读8K字节 81 int count = 0; 82 // 开始读取上传文件的字节,并将其输出到服务端的上传文件输出流中 83 while ((count = is.read(buffer)) > 0) 84 { 85 fos.write(buffer, 0, count); // 向服务端文件写入字节流 86 87 } 88 fos.close(); // 关闭FileOutputStream对象 89 is.close(); // InputStream对象 90 out.println("文件上传成功!"); 91 92 } 93 } 94 catch (Exception e) 95 { 96 97 } 98 } 99 }
服务器的web.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 <servlet> 12 <description></description> 13 <display-name>UploadServlet</display-name> 14 <servlet-name>UploadServlet</servlet-name> 15 <servlet-class>net.blogjava.mobile.UploadServlet</servlet-class> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>UploadServlet</servlet-name> 19 <url-pattern>/UploadServlet</url-pattern> 20 </servlet-mapping> 21 </web-app>
下面是安卓的程序:
1 private void uploadFile(String srcPath) 2 { 3 4 String uploadUrl = "http://10.19.1.83:8080/uploadd/UploadServlet"; 5 String end = "\r\n"; 6 String twoHyphens = "--"; 7 String boundary = "******"; 8 try 9 { 10 URL url = new URL(uploadUrl); 11 HttpURLConnection httpURLConnection = (HttpURLConnection) url 12 .openConnection(); 13 httpURLConnection.setDoInput(true); 14 httpURLConnection.setDoOutput(true); 15 httpURLConnection.setUseCaches(false); 16 httpURLConnection.setRequestMethod("POST"); 17 httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); 18 httpURLConnection.setRequestProperty("Charset", "UTF-8"); 19 httpURLConnection.setRequestProperty("Content-Type", 20 "multipart/form-data;boundary=" + boundary); 21 22 DataOutputStream dos = new DataOutputStream(httpURLConnection 23 .getOutputStream()); 24 dos.writeBytes(twoHyphens + boundary + end); 25 dos 26 .writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" 27 + srcPath.substring(srcPath.lastIndexOf("/") + 1) 28 + "\"" + end); 29 String tag = null; 30 Log.d(tag, srcPath+"/"); 31 dos.writeBytes(end); 32 33 FileInputStream fis = new FileInputStream(srcPath); 34 byte[] buffer = new byte[8192]; // 8k 35 int count = -1; 36 while ((count = fis.read(buffer)) != -1) 37 { 38 dos.write(buffer, 0, count); 39 40 } 41 42 dos.writeBytes(end); 43 dos.writeBytes(twoHyphens + boundary + twoHyphens + end); 44 fis.close(); 45 dos.flush(); 46 47 InputStream is = httpURLConnection.getInputStream(); 48 InputStreamReader isr = new InputStreamReader(is, "utf-8"); 49 BufferedReader br = new BufferedReader(isr); 50 String result = br.readLine(); 51 52 Toast.makeText(this, result, Toast.LENGTH_LONG).show(); 53 dos.close(); 54 is.close(); 55 56 } catch (Exception e) 57 { 58 e.printStackTrace(); 59 setTitle(e.getMessage()); 60 } 61 }