HttpURLConnection 传送数据到服务器

 public class HttpConnector extends Properties {
 private HttpURLConnection conn;
 URL u;

 void setURL(String url) {
  try {
   if (u != null)
    return;
   u = new URL(url);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private void connect() throws Exception {
  try {
   
   if (u == null) {
    throw new Exception("URL not set");
   }
   System.out.println("uploading URL is " + u.toString());
   conn = (HttpURLConnection) u.openConnection();
   conn.setDoOutput(true);
   // conn.setRequestProperty("cookie", sessionId + "; ");

//连接远程地址,通过服务器,定位到一个servlet处理类

   conn.connect();
   System.out.println("conncetion is " + conn.toString());
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public Response send() {
  try {
   connect();
   load();
  } catch (Exception e) {
   e.printStackTrace();
   clear();
   put(Constant.ERRORCODE, "003");
   put(Constant.ERRORDESCRIPTION, Constant.error[3]);
  }
  return new Response(this);
 }

 public void add(String key, String value) {
  if (key == null || value == null)
   return;
  put(key, value);
 }

 private void load() throws Exception {
  try {

//传送数据
   OutputStream os = conn.getOutputStream();
   store(os, null);
   os.flush();
   os.close();
   InputStream is = conn.getInputStream();
   clear();
   load(is);
   is.close();
  } catch (Exception e) {
   e.printStackTrace();
   throw new Exception();
  }
 }
}

 

远程的servlet类:

package com.innov8tion.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.innov8tion.exception.LimitedCategoryNumberException;
import com.innov8tion.exception.NeedLoginException;
import com.innov8tion.image.utils.Constant;
import com.innov8tion.image.utils.HttpServerVO;
import com.innov8tion.bo.User;
import com.innov8tion.service.IImageService;
import com.innov8tion.util.Constants;
import com.innov8tion.util.StringUtil;

/**
 * impoo Servlet implementation class for Servlet: ImgCompressServlet
 *
 */
public class ImgCompressServlet extends javax.servlet.http.HttpServlet
  implements javax.servlet.Servlet, Task {

 public ImgCompressServlet() {
  super();
 }

 public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  InputStream in = request.getInputStream();
  OutputStream out = response.getOutputStream();
  
  HttpServerVO vo = new HttpServerVO();
  HttpSession session = request.getSession();
  Object o = session.getAttribute(Constants.USER_KEY);
  User user = null;
  if(o != null && o instanceof User){
    user = (User) o;
  }
  
  // load the data sent from the client in the HttpServerVO(HttpServerVO extends Properties)
  vo.load(in);

  Task t = TaskFactory.getTask((String) vo.get("task"));

  if (t == null) {
   vo.clear();
   vo.put(Constant.ERRORCODE, "004");
   vo.put(Constant.ERRORDESCRIPTION, Constant.error[4]);
   vo.store(out);
   return;
  }
  vo = (HttpServerVO) t.execute(getServletContext(), vo, user);
  vo.store(out);
 }

 /**
  * process the request
  *
  * @param h
  * @return
  */
 public Hashtable execute(ServletContext context, Hashtable h, User user) {
  try {
   if(user == null)
    throw new NeedLoginException();

   WebApplicationContext wac =
    WebApplicationContextUtils.getRequiredWebApplicationContext(context);
   IImageService imageService = (IImageService) wac.getBean("imageService");
   
   String task = (String) h.get("task");
//   String savedFolder = null;
   if (task.equals(Constant.TASK_SAVE)) {
    String imagename = (String) h.get(Constant.IMAGE_NAME);
//    String imagesize = (String) h.get(Constant.IMAGE_SIZE);
    String imagedata = (String) h.get(Constant.IMAGE);    
    String dir_name = (String) h.get(Constant.DIR_NAME);
    int event_id = StringUtil.toInt(h.get(Constant.EVENT_ID), -1);
    
    imageService.saveImage(user.getUser_id(), imagename, imagedata, dir_name, event_id);

    h.clear();
   }
   
   // running to here means DB operation is success
   h.put(Constant.ERRORCODE, "000");
   h.put(Constant.ERRORDESCRIPTION, Constant.error[0]);
//   h.put(Constant.FOLDERNAME, savedFolder);
  }catch(NeedLoginException e){
   e.printStackTrace();
   h.clear();
   h.put(Constant.ERRORCODE, "001");
   h.put(Constant.ERRORDESCRIPTION, Constant.error_needlogin);
  }catch(LimitedCategoryNumberException e){
   h.clear();
   h.put(Constant.ERRORCODE, "001");
   h.put(Constant.ERRORDESCRIPTION, Constant.error_maxcategory);
  }catch (NullPointerException e) {
   // the client send wrong parameters
   e.printStackTrace();
   h.clear();
   h.put(Constant.ERRORCODE, "001");
   h.put(Constant.ERRORDESCRIPTION, Constant.error[1]);   
  } catch (Exception e) {
   // unKnown Server error
   e.printStackTrace();
   h.clear();
   h.put(Constant.ERRORCODE, "005");
   h.put(Constant.ERRORDESCRIPTION, Constant.error[5]);
  }
  return h;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值