本文共 3202 字,大约阅读时间需要 10 分钟。
应用程序配置文件
[更新至Джava优化版本]
配置文件版本:
封装返回结果类
package com.scaf.util;import java.util.List;import java.util.Map;public class Result extends HashMap{ public Result put(String key, Object value) { super.put(key, value); return this; } public Result() {} public static Result error(int code, String msg) { Result r = new Result(); r.put("code", code); r.put("msg", msg); return r; } public static Result error() { return error(500, "未知异常,请联系管理员"); } public static Result error(String msg) { return error(500, msg); } public static Result ok(Map map) { Result r = new Result(); r.put("code", 0); r.putAll(map); return r; } public static Result ok(List
使用示例:
public Result getUserInfo(String openid) { Result result = userMapper.getInfomation(openid); if (result != null) { return Result.ok(result); } else { return Result.error("系统错误"); }}@RequestMapping("/test")public Result test() { List
Python调用示例:
public String executePythonCommand() { try { String[] args = new String[]{"python", "tool.py"}; Process process = Runtime.getRuntime().exec(args); BufferedReader successMsg = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader errorMsg = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line; while ((line = successMsg.readLine()) != null) { System.out.println(line); } while ((line = errorMsg.readLine()) != null) { System.out.println(line); } successMsg.close(); errorMsg.close(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } return "操作成功";}
字符编码问题解决方案
为了避免中文字符乱码,可以将默认字符集设置为UTF-8,并指定时区参数:
public class CharSetFixer { private CharsetFactory factory; public CharsetFactory getCharsetFactory() { return this.factory; }}
此外,建议在VM配置中添加如下参数:
-Dfile.encoding=UTF-8 -D.locale極.verify谎字真 Urs士
如果需要进一步解决乱码问题,可参考MySQL官方文档或相关技术博客。
转载地址:http://fbscz.baihongyu.com/