zoukankan      html  css  js  c++  java
  • Android、iOS与Servlet接口上传文件和JSON串的交互

      1 package etcom.servlet;
      2 
      3 import java.io.File;
      4 import java.io.IOException;
      5 import java.sql.Connection;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import javax.servlet.ServletException;
     10 import javax.servlet.http.HttpServlet;
     11 import javax.servlet.http.HttpServletRequest;
     12 import javax.servlet.http.HttpServletResponse;
     13 
     14 import org.apache.commons.fileupload.FileItem;
     15 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
     16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
     17 import org.apache.commons.lang.StringUtils;
     18 import org.json.JSONArray;
     19 import org.json.JSONObject;
     20 
     21 import etcom.servlet.DBUtil;
     22 import etcom.servlet.SqlActuator;
     23 
     24 @SuppressWarnings("serial")
     25 public class Execute extends HttpServlet {
     26 
     27     public Execute() {
     28         super();
     29     }
     30 
     31     public void destroy() {
     32         super.destroy(); // Just puts "destroy" string in log
     33         // Put your code here
     34     }
     35 
     36     public void doGet(HttpServletRequest request, HttpServletResponse response)
     37             throws ServletException, IOException {
     38         request.setCharacterEncoding("utf-8");
     39 //        String tr = IoToJsonUtils.getIoToJson(request, "UTF-8");
     40 //        System.out.println(tr);
     41         String vcRes = "";
     42         String detectTaskJOSN = "";
     43         List<String> sqls = new ArrayList<String>();
     44         Connection conn = DBUtil.getConnection();
     45         SqlActuator sa = new SqlActuator(conn);
     46         try{
     47             String basePath = request.getSession().getServletContext().getRealPath("/");
     48             String filePath = "../images/androidFiles/";
     49             String filename = "";
     50             File file0 =new File(basePath + filePath);   
     51             if(!file0.exists()&&!file0.isDirectory())file0.mkdirs();
     52             DiskFileItemFactory factory = new DiskFileItemFactory();
     53             factory.setRepository(file0);    
     54             //设置 缓存的大小  
     55             factory.setSizeThreshold(1024*1024);    
     56             //文件上传处理    
     57             ServletFileUpload upload = new ServletFileUpload(factory);
     58             String encoding = request.getCharacterEncoding();
     59             upload.setHeaderEncoding(encoding);
     60             @SuppressWarnings("unchecked")
     61             List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
     62             for(FileItem item : list){    
     63                 //获取属性名字    
     64                 String name = item.getFieldName();    
     65                 //如果获取的 表单信息是普通的 文本 信息    
     66                 if(item.isFormField()&&"detectTaskJOSN".equals(name)){                       
     67                     //获取用户具体输入的字符串,因为表单提交过来的是 字符串类型的    
     68                     detectTaskJOSN = new String(item.getString(encoding));
     69                     System.out.println("detectTaskJOSN的值:"+detectTaskJOSN);
     70                 }else{
     71                     if(StringUtils.isNotEmpty(name)){
     72                         //获取路径名    
     73                         String value = item.getName();    
     74                         //索引到最后一个反斜杠    
     75                         int start = value.lastIndexOf("\");
     76                         //截取 上传文件的 字符串名字,加1是 去掉反斜杠,    
     77                         filename = value.substring(start+1);    
     78                         File file1 =new File(basePath + filePath + filename);
     79                         if(file1.exists())file1.delete();    
     80                         //写到磁盘上    
     81                         item.write(file1);//第三方提供的    
     82                         System.out.println("上传成功:"+filename);
     83                     }
     84                 }    
     85             }
     86             JSONObject joTask = new JSONObject(detectTaskJOSN);
     87             String _vcTaskNo = joTask.get("vcTaskNo").toString();
     88             String _vcUserId = joTask.get("vcUserId").toString();
     89             String _vcEquipCode = joTask.get("vcEquipCode").toString();
     90             String _vcEquipType = joTask.get("vcEquipType").toString();
     91             String _vcStatus = joTask.get("vcStatus").toString();
     92             String _vcMemo = joTask.get("vcMemo").toString();
     93             String _dtScan = joTask.get("dtScan").toString();
     94             String _dtHandle = joTask.get("dtHandle").toString();
     95             String _vcHandleFlag = joTask.get("vcHandleFlag").toString();
     96             String _vcHandleMemo = joTask.get("vcHandleMemo").toString();
     97             String _vcHandler = joTask.get("vcHandler").toString();
     98             String _nTypeId = joTask.get("nTypeId").toString();
     99             String _nOid = joTask.get("nOid").toString();
    100             String _nCodeId = joTask.get("nCodeId").toString();
    101             String sql = " exec proc_detecttask_add '" + _vcTaskNo + "','" + 
    102                     _vcUserId + "','" + _vcEquipCode + "','" + _vcEquipType + 
    103                     "','" + _vcStatus + "','" + _vcMemo + "','" + 
    104                     filename + "','" + _dtScan + "','" + _dtHandle + 
    105                     "','" + _vcHandleMemo + "'," + _vcHandleFlag + ",'" + 
    106                     _vcHandler + "'," + _nTypeId + "," + _nOid + "," + 
    107                     _nCodeId;
    108             System.out.println(sql);
    109             sqls.add(sql);
    110             
    111             JSONArray jaDicts = new JSONArray(joTask.get("jaDicts").toString());
    112             for(int idx = 0; idx < jaDicts.length(); idx++){
    113                 JSONObject joDict = (JSONObject)jaDicts.get(idx);
    114                 String _vcDetectInfo = joDict.get("vcDetectInfo").toString();
    115                 String _vcDetectValue = joDict.get("vcDetectValue").toString();
    116                 String _vcUnit = joDict.get("vcUnit").toString();
    117                 sql = "Insert Into T_DetectTask_Detail(vcTaskNo,vcDetectInfo,vcDetectValue,vcUnit) values ('" + 
    118                   _vcTaskNo + 
    119                   "','" + 
    120                   _vcDetectInfo + 
    121                   "','" + _vcDetectValue + "','" + _vcUnit + "')";
    122                 System.out.println(sql);
    123                 sqls.add(sql);
    124             }
    125             sa.batchExec(sqls);
    126             vcRes = "{"vcRes":"success"}";
    127         }catch(Exception e){
    128             e.printStackTrace();
    129             vcRes = "{"vcRes":"fail"}";
    130         }finally{
    131             DBUtil.close(null, null, conn);
    132             response.getWriter().println(vcRes);
    133         }
    134     }
    135 
    136     public void doPost(HttpServletRequest request, HttpServletResponse response)
    137             throws ServletException, IOException {
    138         
    139         doGet(request, response);
    140     }
    141 
    142     public void init() throws ServletException {
    143         // Put your code here
    144     }
    145 }
  • 相关阅读:
    打包应用和构建Docker镜像(docker在windows上)
    Docker 在Windows上的安装
    Docker Zero Deployment and Secrets (二)
    Docker Zero Deployment and Secrets (一)
    系统基础信息模块psutil之获取系统性能篇
    IPython的安装方法
    用三种方法设置CentOS7使用代理服务器上网
    CentOS7修改pypi源
    CentOS7升级Python3
    CPU频率的简单测试工具
  • 原文地址:https://www.cnblogs.com/zhncnblogs/p/7543362.html
Copyright © 2011-2022 走看看