android上传图片至jsp服务器 – 还是你最好 – 博客园

本实例实现了android上传手机图片至jsp服务器,服务器进行保存,具有一定的参考价值。

复制代码
<span style="color: #000000;">服务器servlet代码
</span><span style="color: #0000ff;">public</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> doPost(HttpServletRequest request, HttpServletResponse response)  
           throws ServletException, IOException {  
            
           String temp</span><span style="color: #000000;">=</span><span style="color: #000000;">request.getSession().getServletContext().getRealPath(</span><span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">temp</span><span style="color: #800000;">"</span><span style="color: #000000;">;   </span><span style="color: #008000;">//</span><span style="color: #008000;">临时目录</span><span style="color: #008000;">
</span><span style="color: #000000;">           System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(</span><span style="color: #800000;">"</span><span style="color: #800000;">temp=</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">temp);
           String loadpath</span><span style="color: #000000;">=</span><span style="color: #000000;">request.getSession().getServletContext().getRealPath(</span><span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">Image</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">上传文件存放目录</span><span style="color: #008000;">
</span><span style="color: #000000;">           System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(</span><span style="color: #800000;">"</span><span style="color: #800000;">loadpath=</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">loadpath);
           DiskFileUpload fu </span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> DiskFileUpload();
           fu.setSizeMax(</span><span style="color: #800080;">1</span><span style="color: #000000;">*</span><span style="color: #800080;">1024</span><span style="color: #000000;">*</span><span style="color: #800080;">1024</span><span style="color: #000000;">);   </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置允许用户上传文件大小,单位:字节</span><span style="color: #008000;">
</span><span style="color: #000000;">           fu.setSizeThreshold(</span><span style="color: #800080;">4096</span><span style="color: #000000;">);   </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置最多只允许在内存中存储的数据,单位:字节</span><span style="color: #008000;">
</span><span style="color: #000000;">           fu.setRepositoryPath(temp); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
          
           </span><span style="color: #008000;">//</span><span style="color: #008000;">开始读取上传信息</span><span style="color: #008000;">
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> index</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;
           List fileItems </span><span style="color: #000000;">=</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;
                
                        
                                </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
                                        fileItems </span><span style="color: #000000;">=</span><span style="color: #000000;"> fu.parseRequest(request);
                                         System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(</span><span style="color: #800000;">"</span><span style="color: #800000;">fileItems=</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">fileItems);
                                } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
                                        e.printStackTrace();
                                }
                        
                
           Iterator iter </span><span style="color: #000000;">=</span><span style="color: #000000;"> fileItems.iterator(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 依次处理每个上传的文件</span><span style="color: #008000;">
</span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (iter.hasNext())
           {
               FileItem item </span><span style="color: #000000;">=</span><span style="color: #000000;"> (FileItem)iter.next();</span><span style="color: #008000;">//</span><span style="color: #008000;"> 忽略其他不是文件域的所有表单信息</span><span style="color: #008000;">
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">item.isFormField())
               {
                   String name </span><span style="color: #000000;">=</span><span style="color: #000000;"> item.getName();</span><span style="color: #008000;">//</span><span style="color: #008000;">获取上传文件名,包括路径</span><span style="color: #008000;">
</span><span style="color: #000000;">                   name</span><span style="color: #000000;">=</span><span style="color: #000000;">name.substring(name.lastIndexOf(</span><span style="color: #800000;">"</span><span style="color: #800000;">\\</span><span style="color: #800000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">从全路径中提取文件名</span><span style="color: #008000;">
</span><span style="color: #0000ff;">long</span><span style="color: #000000;"> size </span><span style="color: #000000;">=</span><span style="color: #000000;"> item.getSize();
                   </span><span style="color: #0000ff;">if</span><span style="color: #000000;">((name</span><span style="color: #000000;">==</span><span style="color: #0000ff;">null</span><span style="color: #000000;">||</span><span style="color: #000000;">name.equals(</span><span style="color: #800000;">""</span><span style="color: #000000;">)) </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> size</span><span style="color: #000000;">==</span><span style="color: #800080;">0</span><span style="color: #000000;">)
                         </span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;
                   </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> point </span><span style="color: #000000;">=</span><span style="color: #000000;"> name.indexOf(</span><span style="color: #800000;">"</span><span style="color: #800000;">.</span><span style="color: #800000;">"</span><span style="color: #000000;">);
                   name</span><span style="color: #000000;">=</span><span style="color: #000000;">(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Date()).getTime()</span><span style="color: #000000;">+</span><span style="color: #000000;">name.substring(point,name.length())</span><span style="color: #000000;">+</span><span style="color: #000000;">index;
                   index</span><span style="color: #000000;">++</span><span style="color: #000000;">;
                   File fNew</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> File(loadpath, name);
                   </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
                                        item.write(fNew);
                                } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
                                        </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span><span style="color: #008000;">
</span><span style="color: #000000;">                                        e.printStackTrace();
                                }
                  
                  
               }
               </span><span style="color: #0000ff;">else</span><span style="color: #008000;">//</span><span style="color: #008000;">取出不是文件域的所有表单信息</span><span style="color: #008000;">
</span><span style="color: #000000;">               {
                   String fieldvalue </span><span style="color: #000000;">=</span><span style="color: #000000;"> item.getString();
            </span><span style="color: #008000;">//</span><span style="color: #008000;">如果包含中文应写为:(转为UTF-8编码)
                   </span><span style="color: #008000;">//</span><span style="color: #008000;">String fieldvalue = new String(item.getString().getBytes(),"UTF-8");</span><span style="color: #008000;">
</span><span style="color: #000000;">               }
           }
           String text1</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">11</span><span style="color: #800000;">"</span><span style="color: #000000;">;
           response.sendRedirect(</span><span style="color: #800000;">"</span><span style="color: #800000;">result.jsp?text1=</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;"> text1);
    }  </span>
复制代码

android客户端代码

复制代码
<span style="color: #0000ff;">public</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> PhotoUpload </span><span style="color: #0000ff;">extends</span><span style="color: #000000;"> Activity {
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String newName </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">image.jpg</span><span style="color: #000000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String uploadFile </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">/sdcard/image.JPG</span><span style="color: #000000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String actionUrl </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">http://192.168.0.71:8086/HelloWord/myForm</span><span style="color: #000000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> TextView mText1;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> TextView mText2;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Button mButton;
    @Override
      </span><span style="color: #0000ff;">public</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> onCreate(Bundle savedInstanceState)
      {
        </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.onCreate(savedInstanceState);
        setContentView(R.layout.photo_upload);
        mText1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> (TextView) findViewById(R.id.myText2);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">"文件路径:\n"+</span><span style="color: #008000;">
</span><span style="color: #000000;">        mText1.setText(uploadFile);
        mText2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> (TextView) findViewById(R.id.myText3);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">"上传网址:\n"+</span><span style="color: #008000;">
</span><span style="color: #000000;">        mText2.setText(actionUrl);
        </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 设置mButton的onClick事件处理 </span><span style="color: #008000;">*/</span><span style="color: #000000;">    
        mButton </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Button) findViewById(R.id.myButton);
        mButton.setOnClickListener(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> View.OnClickListener()
        {
          </span><span style="color: #0000ff;">public</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> onClick(View v)
          {
            uploadFile();
          }
        });
      }
      </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 上传文件至Server的方法 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
      </span><span style="color: #0000ff;">private</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> uploadFile()
      {
        String end </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">\r\n</span><span style="color: #000000;">"</span><span style="color: #000000;">;
        String twoHyphens </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">--</span><span style="color: #000000;">"</span><span style="color: #000000;">;
        String boundary </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">*****</span><span style="color: #000000;">"</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;">
        {
          URL url </span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> URL(actionUrl);
          HttpURLConnection con</span><span style="color: #000000;">=</span><span style="color: #000000;">(HttpURLConnection)url.openConnection();
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 允许Input、Output,不使用Cache </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          con.setDoInput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
          con.setDoOutput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
          con.setUseCaches(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">);
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 设置传送的method=POST </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          con.setRequestMethod(</span><span style="color: #000000;">"</span><span style="color: #000000;">POST</span><span style="color: #000000;">"</span><span style="color: #000000;">);
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> setRequestProperty </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          con.setRequestProperty(</span><span style="color: #000000;">"</span><span style="color: #000000;">Connection</span><span style="color: #000000;">"</span><span style="color: #000000;">, </span><span style="color: #000000;">"</span><span style="color: #000000;">Keep-Alive</span><span style="color: #000000;">"</span><span style="color: #000000;">);
          con.setRequestProperty(</span><span style="color: #000000;">"</span><span style="color: #000000;">Charset</span><span style="color: #000000;">"</span><span style="color: #000000;">, </span><span style="color: #000000;">"</span><span style="color: #000000;">UTF-8</span><span style="color: #000000;">"</span><span style="color: #000000;">);
          con.setRequestProperty(</span><span style="color: #000000;">"</span><span style="color: #000000;">Content-Type</span><span style="color: #000000;">"</span><span style="color: #000000;">,
                             </span><span style="color: #000000;">"</span><span style="color: #000000;">multipart/form-data;boundary=</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">boundary);
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 设置DataOutputStream </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          DataOutputStream ds </span><span style="color: #000000;">=</span><span style="color: #000000;">
            </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> DataOutputStream(con.getOutputStream());
          ds.writeBytes(twoHyphens </span><span style="color: #000000;">+</span><span style="color: #000000;"> boundary </span><span style="color: #000000;">+</span><span style="color: #000000;"> end);
          ds.writeBytes(</span><span style="color: #000000;">"</span><span style="color: #000000;">Content-Disposition: form-data; </span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">
                        </span><span style="color: #000000;">"</span><span style="color: #000000;">name=\"file1\";filename=\"</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">
                        newName </span><span style="color: #000000;">+</span><span style="color: #000000;">"</span><span style="color: #000000;">\"</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;"> end);
          ds.writeBytes(end);  
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 取得文件的FileInputStream </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          FileInputStream fStream </span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(uploadFile);
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 设置每次写入1024bytes </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> bufferSize </span><span style="color: #000000;">=</span><span style="color: #000000;">1024</span><span style="color: #000000;">;
          </span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] buffer </span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[bufferSize];
          </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> length </span><span style="color: #000000;">=</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 从文件读取数据至缓冲区 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          </span><span style="color: #0000ff;">while</span><span style="color: #000000;">((length </span><span style="color: #000000;">=</span><span style="color: #000000;"> fStream.read(buffer)) </span><span style="color: #000000;">!=</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)
          {
            </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 将资料写入DataOutputStream中 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
            ds.write(buffer, </span><span style="color: #000000;">0</span><span style="color: #000000;">, length);
          }
          ds.writeBytes(end);
          ds.writeBytes(twoHyphens </span><span style="color: #000000;">+</span><span style="color: #000000;"> boundary </span><span style="color: #000000;">+</span><span style="color: #000000;"> twoHyphens </span><span style="color: #000000;">+</span><span style="color: #000000;"> end);
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> close streams </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          fStream.close();
          ds.flush();
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 取得Response内容 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          InputStream is </span><span style="color: #000000;">=</span><span style="color: #000000;"> con.getInputStream();
          </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ch;
          StringBuffer b </span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuffer();
          </span><span style="color: #0000ff;">while</span><span style="color: #000000;">( ( ch </span><span style="color: #000000;">=</span><span style="color: #000000;"> is.read() ) </span><span style="color: #000000;">!=</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"> )
          {
            b.append( (</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)ch );
          }
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 将Response显示于Dialog </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          showDialog(</span><span style="color: #000000;">"</span><span style="color: #000000;">上传成功</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">b.toString().trim());
          </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 关闭DataOutputStream </span><span style="color: #008000;">*/</span><span style="color: #000000;">
          ds.close();
        }
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e)
        {
          showDialog(</span><span style="color: #000000;">"</span><span style="color: #000000;">上传失败</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">e);
        }
      }
      </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 显示Dialog的method </span><span style="color: #008000;">*/</span><span style="color: #000000;">
      </span><span style="color: #0000ff;">private</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> showDialog(String mess)
      {
        </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AlertDialog.Builder(PhotoUpload.</span><span style="color: #0000ff;">this</span><span style="color: #000000;">).setTitle(</span><span style="color: #000000;">"</span><span style="color: #000000;">Message</span><span style="color: #000000;">"</span><span style="color: #000000;">)
         .setMessage(mess)
         .setNegativeButton(</span><span style="color: #000000;">"</span><span style="color: #000000;">确定</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> DialogInterface.OnClickListener()
         {
           </span><span style="color: #0000ff;">public</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> onClick(DialogInterface dialog, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> which)
           {          
           }
         })
         .show();
      }
    }</span>
复制代码

 

来源URL:http://www.cnblogs.com/gzggyy/archive/2011/08/02/2125237.html