发表于: 2018-03-17 20:53:13
1 628
今日完成
1.文件上传的工具类编写
/**
* @author lujing
* Create_at 2018/3/17 9:54
*/
public class FilesUtil {
private static final Logger logger = Logger.getLogger(FilesUtil.class);
// 1 初始化用户身份信息(secretId, secretKey)
static COSCredentials cred = new BasicCOSCredentials("AKID1wskR7t8MPI1kNuj9ayDH8***", "DID6pMkw9RL8mHepobGm*******");
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
static ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
// 3 生成cos客户端
static COSClient cosClient = new COSClient(cred, clientConfig);
// bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
static String bucketName = "lujing0613-******";
//服务器根目录
static String rootPATH = "https://lujing0613-1255932852.cos.ap-chengdu.******";
/**
* @param picFile springmvc上传的图片流
* @return 云储存路径(非访问路径)
* @throws IOException
*/
public static String upLoadFile(MultipartFile picFile) throws IOException {
if (picFile != null) {
//获得文件的原始名字
String originalFileName = picFile.getOriginalFilename();
//新的文件名称=uuid+原始名字的后缀.xxx
String newFlieName = UUID.randomUUID() + originalFileName.substring(originalFileName.indexOf("."));
//储存地址
String key = "student/" + newFlieName;
//将MultipartFile转换为file.
try {
//创建一个临时文件
File temp = File.createTempFile("temp", null);
//将MultipartFile 写入临时文件
picFile.transferTo(temp);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, temp);
cosClient.putObject(putObjectRequest);
logger.info("成功:上传地址:" + key);
temp.delete();
temp.deleteOnExit();
cosClient.shutdown();
return key;
} catch (IOException e) {
e.printStackTrace();
logger.info("......图片上传失败");
}
}
return null;
}
/**
* @param file 普通的文件流
* @return 云储存路径(非访问路径)
* @throws IOException
*/
public static String upLoadFile(File file) throws IOException {
//获得文件的原始名字
String originalFileName = file.getName();
//新的文件名称=uuid+原始名字的后缀.xxx
String newFlieName = UUID.randomUUID() + originalFileName.substring(originalFileName.indexOf("."));
//储存地址
String key = "student/" + newFlieName;
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
cosClient.putObject(putObjectRequest);
cosClient.shutdown();
return key;
}
/**
* @param FilePath 数据库储存路径
* @throws CosServiceException
*/
public static void deleteObject(String FilePath) throws CosServiceException {
// //处理字符串,去掉 rootPATH
// String cosPath = oldFilePath.replace(rootPATH, "");
cosClient.deleteObject(bucketName, FilePath);
cosClient.shutdown();
}
public static String getUrl(String FilePath) {
//处理字符串,去掉 rootPATH
// String cosPath = oldFilePath.replace(rootPATH, "");
String key = FilePath;
GeneratePresignedUrlRequest req =
new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
req.setExpiration(expirationDate);
URL downloadUrl = cosClient.generatePresignedUrl(req);
String downloadUrlStr = downloadUrl.toString();
return downloadUrlStr;
}
}
2.动态查询条件的拼接工具
/**
*
* @param name 姓名
* @param phoneNum 手机号
* @param accountsStatus 账户状态
* @param registerDateStart 注册日期起
* @param registerDateEnd 注册日期止
* @param managerNum
* @return 查询条件
*/
public static Map<String, Object> getUserListSql(String name, String phoneNum, Integer accountsStatus ,Long registerDateStart,Long registerDateEnd,String managerNum) {
Map<String, Object> param = new HashMap<>();
if (name != null && !name.equals("")) {
param.put("name & like", "'%" + name + "%'");
}
if (phoneNum != null && !phoneNum.equals("")) {
param.put("phoneNum & like", "'%" + phoneNum + "%'");
}
if (phoneNum != null && !phoneNum.equals("")) {
param.put("phoneNum & like", "'%" + phoneNum + "%'");
}
param.put("@table", "user");
return param;
}
3.小课堂,复习了几种生成json串的方式
@Controller
@RequestMapping
public class JsonController {
@RequestMapping(value = "/test1")
public @ResponseBody
String test1(Model model) {
model.addAttribute("message", "hell spring mvc ");
//使用jsonobject的方式
JSONObject a = new JSONObject();
a.put("name", "张三");
a.put("age", 14);
JSONObject b = new JSONObject();
b.put("name", "张si");
JSONArray c = new JSONArray();
c.add(a);
c.add(b);
return c.toString();
}
@RequestMapping(value = "/test2")
public @ResponseBody Object test2() {
//使用@ResponseBody 将java实体类对象转换为json. 实体类中的属性必须有set和get方法才可以。
Person a = new Person();
a.setName("lisi");
a.setAddress("cichuang");
a.setAge(12);
Person2 b = new Person2();
b.name = "zhangsan";
b.age = "12";
return a;
}
@RequestMapping(value = "/test3")
public @ResponseBody
Object test3() {
//使用@ResponseBody 将数组转换为json.
Person a = new Person();
a.setName("lisi");
a.setAddress("cichuang");
a.setAge(12);
Person a1 = new Person();
a1.setName("lisi");
a1.setAddress("cichuang");
a1.setAge(12);
List<Person> b = new ArrayList<>();
b.add(a);
b.add(a1);
return b;
}
@RequestMapping(value = "/test4")
public String test4(Model model) {
Person a = new Person();
a.setName("lisi");
a.setAddress("cichuang");
a.setAge(12);
Person a1 = new Person();
a1.setName("lisi");
a1.setAddress("cichuang");
a1.setAge(12);
List<Person> b = new ArrayList<>();
b.add(a);
b.add(a1);
model.addAttribute("message", "chenggong");
model.addAttribute("data", b);
return "/jsonTag";
}
@RequestMapping(value = "/test5")
@ResponseBody
public void test5( Person person) {
System.out.println(person);
}
@RequestMapping(value = "/test6")
public void test6() {
System.out.println("dayin ");
}
}
明日计划
1.继续写工具类和接口
收获
1.了解了公司框架动态查询的思路
遇到问题
1.json-taglib的包。导入失败。
评论