Package demo.servlet

Source Code of demo.servlet.UploadPhotoServlet

package demo.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.param.impl.AccessToken;
import com.renren.api.client.services.RenrenApiException;

/**
* Servlet implementation class UploadPhotoServlet
*/
public class UploadPhotoServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadPhotoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
      RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/uploadphoto.jsp");
        welcomeDispatcher.forward(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
      String flag=(String)request.getParameter("flag");
      String accessToken=(String)request.getSession().getAttribute("access_token");
      if(flag!=null&&accessToken!=null&&!"".equals(flag)&&!"".equals(accessToken)){
          RenrenApiClient client=RenrenApiClient.getInstance();
          if(flag.equals("local")){
              //上传本地图片
              String filename=(String)request.getParameter("filename");
              filename=new String(filename.getBytes("ISO8859-1"),"UTF-8");
              try{
                  client.getPhotoService().uploadLocalImg(0, filename, "测试上传本地图片"+new Date(),new AccessToken(accessToken));
              }catch(Exception e){
                  request.setAttribute("errMsg", e.getMessage());
                    RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/error.jsp");
                    welcomeDispatcher.forward(request, response);
              }
             
          }else if(flag.equals("url")){
              //上传网络图片
              String fileurl=(String)request.getParameter("fileurl");
              try{
                  client.getPhotoService().uploadURLImg(0, fileurl, "测试上传网络图片"+new Date(),new AccessToken(accessToken));
              }catch(Exception e){
                  request.setAttribute("errMsg", e.getMessage());
                    RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/error.jsp");
                    welcomeDispatcher.forward(request, response);
              }
          }
      }else{
          //
          request.setAttribute("errMsg", "缺少accessToken");
            RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/error.jsp");
            welcomeDispatcher.forward(request, response);
      }
  }

}
TOP

Related Classes of demo.servlet.UploadPhotoServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.