android手机客户端上传文件,java servlet服务器端接收并保存到服务器

来源:互联网 发布:傲剑坐骑数据 编辑:程序博客网 时间:2024/05/01 09:16
android客户端代码: 
Java代码  收藏代码
  1. public class MainActivity extends Activity  
  2. {  
  3.     private TextView uploadInfo;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.           
  11.         uploadInfo = (TextView) findViewById(R.id.upload_info);  
  12.   
  13.         uploadFile();  
  14.     }  
  15.   
  16.     public void uploadFile()  
  17.     {  
  18.         //服务器端地址  
  19.         String url = "http://192.168.0.108:8080/UploadFileServer/upload";  
  20.         //手机端要上传的文件,首先要保存你手机上存在该文件  
  21.         String filePath = Environment.getExternalStorageDirectory()  
  22.                 + "/1/power.apk";  
  23.   
  24.         AsyncHttpClient httpClient = new AsyncHttpClient();  
  25.   
  26.         RequestParams param = new RequestParams();  
  27.         try  
  28.         {  
  29.             param.put("file"new File(filePath));  
  30.             param.put("content""liucanwen");  
  31.               
  32.             httpClient.post(url, param, new AsyncHttpResponseHandler()  
  33.             {  
  34.                 @Override  
  35.                 public void onStart()  
  36.                 {  
  37.                     super.onStart();  
  38.                       
  39.                     uploadInfo.setText("正在上传...");  
  40.                 }  
  41.                   
  42.                 @Override  
  43.                 public void onSuccess(String arg0)  
  44.                 {  
  45.                     super.onSuccess(arg0);  
  46.   
  47.                     Log.i("ck""success>" + arg0);  
  48.                       
  49.                     if(arg0.equals("success"))  
  50.                     {  
  51.                         Toast.makeText(MainActivity.this"上传成功!"1000).show();  
  52.                     }  
  53.                       
  54.                     uploadInfo.setText(arg0);  
  55.                 }  
  56.                   
  57.                 @Override  
  58.                 public void onFailure(Throwable arg0, String arg1)  
  59.                 {  
  60.                     super.onFailure(arg0, arg1);  
  61.                       
  62.                     uploadInfo.setText("上传失败!");  
  63.                 }  
  64.             });  
  65.               
  66.         } catch (FileNotFoundException e)  
  67.         {  
  68.             e.printStackTrace();  
  69.             Toast.makeText(MainActivity.this"上传文件不存在!"1000).show();  
  70.         }  
  71.     }  
  72. }  



服务器端代码: 
Java代码  收藏代码
  1. public class UploadFileServlet extends HttpServlet  
  2. {  
  3.   
  4.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  5.             throws ServletException, IOException  
  6.     {  
  7.         response.setContentType("text/html");  
  8.         PrintWriter out = response.getWriter();  
  9.   
  10.         // 创建文件项目工厂对象  
  11.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  12.   
  13.         // 设置文件上传路径  
  14.         String upload = this.getServletContext().getRealPath("/upload/");  
  15.         // 获取系统默认的临时文件保存路径,该路径为Tomcat根目录下的temp文件夹  
  16.         String temp = System.getProperty("java.io.tmpdir");  
  17.         // 设置缓冲区大小为 5M  
  18.         factory.setSizeThreshold(1024 * 1024 * 5);  
  19.         // 设置临时文件夹为temp  
  20.         factory.setRepository(new File(temp));  
  21.         // 用工厂实例化上传组件,ServletFileUpload 用来解析文件上传请求  
  22.         ServletFileUpload servletFileUpload = new ServletFileUpload(factory);  
  23.   
  24.         // 解析结果放在List中  
  25.         try  
  26.         {  
  27.             List<FileItem> list = servletFileUpload.parseRequest(request);  
  28.   
  29.             for (FileItem item : list)  
  30.             {  
  31.                 String name = item.getFieldName();  
  32.                 InputStream is = item.getInputStream();  
  33.   
  34.                 if (name.contains("content"))  
  35.                 {  
  36.                     System.out.println(inputStream2String(is));  
  37.                 } else if(name.contains("file"))  
  38.                 {  
  39.                     try  
  40.                     {  
  41.                         inputStream2File(is, upload + "\\" + item.getName());  
  42.                     } catch (Exception e)  
  43.                     {  
  44.                         e.printStackTrace();  
  45.                     }  
  46.                 }  
  47.             }  
  48.               
  49.             out.write("success");  
  50.         } catch (FileUploadException e)  
  51.         {  
  52.             e.printStackTrace();  
  53.             out.write("failure");  
  54.         }  
  55.   
  56.         out.flush();  
  57.         out.close();  
  58.     }  
  59.   
  60.     // 流转化成字符串  
  61.     public static String inputStream2String(InputStream is) throws IOException  
  62.     {  
  63.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  64.         int i = -1;  
  65.         while ((i = is.read()) != -1)  
  66.         {  
  67.             baos.write(i);  
  68.         }  
  69.         return baos.toString();  
  70.     }  
  71.   
  72.     // 流转化成文件  
  73.     public static void inputStream2File(InputStream is, String savePath)  
  74.             throws Exception  
  75.     {  
  76.         System.out.println("文件保存路径为:" + savePath);  
  77.         File file = new File(savePath);  
  78.         InputStream inputSteam = is;  
  79.         BufferedInputStream fis = new BufferedInputStream(inputSteam);  
  80.         FileOutputStream fos = new FileOutputStream(file);  
  81.         int f;  
  82.         while ((f = fis.read()) != -1)  
  83.         {  
  84.             fos.write(f);  
  85.         }  
  86.         fos.flush();  
  87.         fos.close();  
  88.         fis.close();  
  89.         inputSteam.close();  
  90.           
  91.     }  
  92.   
  93. }  
1 0