Java工具类:简化Web开发的实用方法
package com.ideabobo.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import com.ideabobo.StartGenerator;
public class Common {
public static Object getByRequestSetfile(Object model,String fileField,String fileNames,HttpServletRequest request,boolean jsonp){
//Object model = c.getInterfaces();
Class c = model.getClass();
Map<String,String> map = new HashMap();
if(fileNames!=null){
if(StringUtil.isNotNullOrEmpty(fileField)){
map.put(fileField, fileNames);
}else{
map.put('img', fileNames);
}
}
Map<String,String[]> parammap = request.getParameterMap();
for(Map.Entry<String, String[]> entry:parammap.entrySet()){
String[] value = entry.getValue();
if(value.length==1){
map.put(entry.getKey(),value[0]);
}
}
Field[] fields = c.getDeclaredFields();
try {
for(int i=0;i<fields.length;i++){
Field field = fields[i];
field.setAccessible(true);
String fname = field.getName();
String mvalue = map.get(fname);
if(mvalue != null && !mvalue.equals('') && !mvalue.equals('null')){
String ftype = field.getType().getSimpleName();
if(ftype.equals('String')){
if(jsonp){
field.set(model, encodeGet(mvalue));
}else{
field.set(model, mvalue);
}
}else if(ftype.equals('Integer') || ftype.equals('int')){
field.set(model, Integer.parseInt(mvalue));
}else if(ftype.equals('Double')){
field.set(model, Double.parseDouble(mvalue));
}else if(ftype.equals('Timestamp')){
Timestamp timestamp = Timestamp.valueOf(mvalue);
field.set(model,timestamp);
}
}
}
}catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
public static Object getByRequest(Object model,HttpServletRequest request,boolean jsonp){
//Object model = c.getInterfaces();
if (model==null){
return null;
}
Class c = model.getClass();
Map<String,String> map = new HashMap();
Map<String,String[]> parammap = request.getParameterMap();
for(Map.Entry<String, String[]> entry:parammap.entrySet()){
String[] value = entry.getValue();
if(value.length==1){
map.put(entry.getKey(),value[0]);
}
}
Field[] fields = c.getDeclaredFields();
try {
for(int i=0;i<fields.length;i++){
Field field = fields[i];
field.setAccessible(true);
String fname = field.getName();
String mvalue = map.get(fname);
if(mvalue != null && !mvalue.equals('') && !mvalue.equals('null')){
String ftype = field.getType().getSimpleName();
if(ftype.equals('String')){
if(jsonp){
field.set(model, encodeGet(mvalue));
}else{
field.set(model, mvalue);
}
}else if(ftype.equals('Integer') || ftype.equals('int')){
field.set(model, Integer.parseInt(mvalue));
}else if(ftype.equals('Double')){
field.set(model, Double.parseDouble(mvalue));
}else if(ftype.equals('Timestamp')){
Timestamp timestamp = Timestamp.valueOf(mvalue);
field.set(model,timestamp);
}
}
}
}catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
public static String encodeGet(String str){
if(str!=null){
try {
str = new String(str.getBytes('iso8859-1'),'utf-8');
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return str;
}
public static String copyFile2Upload(MultipartFile[] files) throws Exception{
String[] remoteNames = new String[files.length];
String fileNames = '';
String path = ResourceUtils.getURL('classpath:').getPath();
String tagetPath = path+'static/upload/';
String preplace = 'target/classes';
String targetSrcPath = tagetPath.replace(preplace,'src/main/resources');
File targetDir = new File(tagetPath);
if (!targetDir.exists()) {
targetDir.mkdirs();
}
/*if (!targetSrcDir.exists()) {
targetSrcDir.mkdirs();
}*/
// 循环获取files数组中的文件
for (int i = 0; i < files.length; i++) {
if (i > 0) {
// 循环太快,会导致时间戳一样
Thread.sleep(1);
}
MultipartFile file = files[i];
String realName = file.getOriginalFilename();
String filePfix = realName.substring(realName.lastIndexOf('.'),realName.length());
remoteNames[i] = UUID.randomUUID() + filePfix; // 服务器端存储的名字
fileNames += remoteNames[i] + ',';
}
// 不加try,catch上传失败,回滚
for (int i = 0; i < files.length; i++) {
MultipartFile file = files[i];
InputStream in = file.getInputStream();
byte[] buffer = new byte[in.available()];
in.read(buffer);
File targetFile = new File(tagetPath+remoteNames[i]);
File targetSrcFile = new File(targetSrcPath+remoteNames[i]);
OutputStream outStream = new FileOutputStream(targetFile);
OutputStream outSrcStream = new FileOutputStream(targetSrcFile);
outStream.write(buffer);
outSrcStream.write(buffer);
if(in!=null){
in.close();
}
if(outStream!=null){
outStream.close();
outSrcStream.close();
}
}
// 返回信息
fileNames = fileNames.substring(0, fileNames.length() - 1);// 去掉最后一个,
return fileNames;
}
public static Properties properties = null;
public static String getProperty(String key){
String r = null;
if(properties!=null){
r = properties.getProperty(key);
}else{
properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = Common.class.getClassLoader()
.getResourceAsStream('application.properties');
if(in!=null){
try {
properties.load(in);
r = properties.getProperty(key);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return r;
}
public static String toUpperCaseFirstOne(String s){
if(Character.isUpperCase(s.charAt(0)))
return s;
else
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
}
public static void str2File(String path,String str){
try {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path),'UTF-8');
osw.write(str);
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码说明:
这段代码定义了一个名为 Common 的 Java 工具类,其中包含以下常用方法:
- getByRequestSetfile: 解析 HttpServletRequest 请求参数,并将参数值设置到 Java 对象属性中,同时支持文件上传处理。
- getByRequest: 解析 HttpServletRequest 请求参数,并将参数值设置到 Java 对象属性中。
- encodeGet: 将 ISO-8859-1 编码的字符串转换为 UTF-8 编码。
- copyFile2Upload: 将上传的文件复制到指定目录,并返回文件名。
- getProperty: 从配置文件中读取指定键的值。
- toUpperCaseFirstOne: 将字符串的首字母转换为大写。
- str2File: 将字符串写入到指定文件中。
使用方法:
可以直接调用 Common 类中的静态方法,例如:
String value = Common.getProperty('keyName');
String encodedString = Common.encodeGet('需要编码的字符串');
总结:
Common 工具类提供了一些 Web 开发中常用的实用方法,可以帮助开发者简化代码编写,提高开发效率。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/jodc 著作权归作者所有。请勿转载和采集!