|
楼主 |
发表于 2016-3-3 17:10:15
|
显示全部楼层
在baas中的对应的java建立自定义的action
怎么建立和定义action请参考起步官方视频WeX5后台调用
- //接收前台传过来的excel,存到数据库
- public static JSONObject myUploadExcel(JSONObject params, ActionContext context) throws SQLException, NamingException {
- HttpServletRequest request = (HttpServletRequest)context.get(ActionContext.REQUEST);
- HashMap<String,String> fields = new HashMap<String,String>(); //用于存放前台传过来的input参数
- InputStream is = null; //用于存放前台传过来的二进制文件输入流
- JSONObject ret=new JSONObject();
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- String values="";
-
- try {
-
- @SuppressWarnings("unchecked")
- List<FileItem> items = upload.parseRequest(request); //把用form提交过来的request转换为list
- Iterator<FileItem> iter = items.iterator();
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (!item.isFormField()) { //如果是二进制文件输入流
- is = item.getInputStream();
- }else{ //如果是input参数,把input参数放到fields的map对象里面
- String name = item.getFieldName();
- String value = URLDecoder.decode(item.getString(), "UTF-8").replaceAll("\\\", "/");
-
- fields.put(name, value); //把表单域的名字和值放到params中
- System.out.println(fields.get("fileName"));
- }
- }
- if(is==null || fields.get("fileName")==null||fields.get("fileName").length()<=0){ //如果文件为空或者文件名或者examBankID为空,则返回上传文件失败
- ret.put("result", "失败");
- }else{
-
- // 把excel内容传到数据库中
- Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(is); //将二进制文件输入流转换为excelWorkbook(兼容2003和2007格式)
- Sheet sheet=workbook.getSheetAt(0); //拿到excel工作簿中的第一个sheet
- int rows=sheet.getPhysicalNumberOfRows(); //计算sheet中有多少行
- for(int i=1;i<rows;i++){ //遍历sheet中的行,从第二行开始
- org.apache.poi.ss.usermodel.Row row=sheet.getRow(i); //拿到第i行
- if(row!=null){ //拼要插到sql语句中的值,因为是mySql所以简单些
- values+="(UUID(),0,'fExamBankID',"+i+",'"+row.getCell(0)+"','"+row.getCell(1)+"','','"+row.getCell(6)+"','"+row.getCell(2)+"','"+row.getCell(3)+"','"+row.getCell(4)+"','"+row.getCell(5)+"'),";
-
- }
-
- }
- values=values.substring(0, values.length()-1); //去掉最后的逗号
- values="insert into examquestion (fID,version,fExamBankID,fNO,fType,fQuestion,fQuestionPic,fCorrectAnswer,fOptionA,fOptionB,fOptionC,fOptionD)"+
- "values"+ values;
- Connection conn= context.getConnection("fishbearWeixin"); //拿到数据库连接
- PreparedStatement pstmt = conn.prepareStatement(values); //执行插入
- pstmt.executeUpdate();
- pstmt.close();
- }
- is.close();
- ret.put("result", "成功");
- } catch (FileUploadException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ret;
- }
复制代码 |
|