public static boolean deployProcess(String requestUrl, Map<String, Object> paramsMap) {
PostMethod filePost = new PostMethod(requestUrl);
int requestStatus = 0;
try {
List<Part> partList = setParameter(paramsMap);
Part[] partArray = partList.toArray(new Part[partList.size()]);
filePost.setRequestEntity(new MultipartRequestEntity(partArray, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
requestStatus = client.executeMethod(filePost);
} catch (Exception e) {
e.printStackTrace();
}
return requestStatus == HttpStatus.SC_OK ? true : false;
}
|
public void upload(String requestUrl, List<String> fileList) {
try {
String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线
int leng = fileList.size();
for (int i = 0; i < leng; i++) {
String fname = (String) fileList.get(i);
File file = new File(fname);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("--").append(BOUNDARY).append("\r\n").append("Content-Disposition: form-data;name=\"file" + i + "\";filename=\"" + file.getName() + "\"\r\n").append("Content-Type:application/octet-stream\r\n\r\n");
byte[] data = stringBuilder.toString().getBytes();
out.write(data);
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.write("\r\n".getBytes()); // 多个文件时,二个文件之间加入这个
in.close();
}
out.write(end_data);
out.flush();
out.close();
// 定义BufferedReader输入流来读取URL的响应
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
|
public <E> PageResult<E> listEntity(Class<E> entityClazz, Integer startIndex, Integer maxResult, String wherehql,
Object[] values, LinkedHashMap<String, String> orderBy) {
Session session = null;
PageResult<E> pageResult = null;
try {
session = this.getSession();
// 获取当前页面的list数据
StringBuilder listHql = new StringBuilder();
listHql.append("select o from ").append(entityClazz.getSimpleName()).append(" o ")
.append(wherehql == null ? "" : "where " + wherehql).append(buildOrderByHql(orderBy));
Query query = session.createQuery(listHql.toString());
// 如果不需要分页查询,startIndex和maxResult设为null或-1
if (startIndex != null && startIndex != -1 && maxResult != null && maxResult != -1) {
query.setFirstResult(startIndex).setMaxResults(maxResult);
}
setQueryValue(query, values);
pageResult = new PageResult<E>();
List<E> listResult = query.list();
if (listResult != null) {
pageResult.setListResult(listResult);
}
// 获取总记录数
StringBuilder countHql = new StringBuilder();
countHql.append("select count(o) from ").append(entityClazz.getSimpleName()).append(" o where 1 = 1 ")
.append(wherehql == null ? "" : "and " + wherehql);
query = session.createQuery(countHql.toString());
setQueryValue(query, values);
Integer totalRecord = (Integer) query.uniqueResult();
if (totalRecord != null) {
pageResult.setTotalRecord(totalRecord);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
return pageResult;
}
private String buildOrderByHql(LinkedHashMap<String, String> orderBy) {
StringBuilder orderByHql = new StringBuilder();
if ((orderBy != null) && (orderBy.size() > 0)) {
orderByHql = orderByHql.append(" order by ");
Set<String> keySet = orderBy.keySet();
for (String key : keySet) {
orderByHql.append(key).append(" ").append(orderBy.get(key)).append(",");
}
orderByHql.deleteCharAt(orderByHql.length() - 1);
}
return orderByHql.toString();
}
private void setQueryValue(Query query, Object[] values) {
if ((values != null) && (values.length > 0)) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
}
|
package com.picc.gz.application.seedmanager.common.web;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import com.picc.gz.application.seedmanager.common.Constants;
public class FullBeanUtil {
private FullBeanUtil() {
}
/**
* 将页面form表单的数据填充到javaBean中
*
* @param request 请求对象
* @param classes 需要填充的class对象
* @return
*/
public static Object setParameter(HttpServletRequest request, Class<?> classes) {
Object object = null;
PropertyDescriptor propertyDescriptor = null;
String fieldName = null;
Method method = null;
try {
object = classes.newInstance();
Field[] fieldArray = classes.getDeclaredFields();
Enumeration<?> enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String enume = (String) enumeration.nextElement();
for (int i = 0; i < fieldArray.length; i++) {
Field field = fieldArray[i];
fieldName = field.getName();
if (enume != null && !Constants.EMPTY.equals(enume)) {
if (enume.equals(fieldName)) {
propertyDescriptor = new PropertyDescriptor(fieldName, classes);
method = propertyDescriptor.getWriteMethod();
Class<?> classzz = field.getType();
setValue(request, method, classzz, object, fieldName);
break;
}
}
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return object;
}
/**
* 设置属性值,主要是用来判断数据类型
*
* @param request 请求对象
* @param method 方法对象
* @param classes javaBean的class对象
* @param object javaBean实例
* @param fieldName 字段名
*/
public static void setValue(HttpServletRequest request, Method method, Class<?> classes, Object object, String fieldName) {
try {
String typName = classes.getName();
String objectValue = request.getParameter(fieldName);
if (typName != null && !Constants.EMPTY.equals(typName)) {
if (typName.equals(Constants.JAVA_LANG_STRING)) {
method.invoke(object, (String) objectValue);
} else if (typName.equals(Constants.JAVA_LANG_INTEGER)) {
method.invoke(object, Integer.parseInt(objectValue));
} else if (typName.equals(Constants.JAVA_UTIL_DATE)) {
DateFormat format = new SimpleDateFormat(Constants.FORMAT_DATE);
method.invoke(object, format.parse(objectValue));
} else if (typName.equals(Constants.JAVA_LANG_LONG)) {
method.invoke(object, Long.parseLong(objectValue));
} else if (typName.equals(Constants.JAVA_LANG_BOOLEAN)) {
method.invoke(object, Boolean.parseBoolean(objectValue));
} else if (typName.equals(Constants.JAVA_LANG_CHAR)) {
method.invoke(object, (String) objectValue);
} else if (typName.equals(Constants.JAVA_LANG_DOUBLE)) {
method.invoke(object, Double.parseDouble(objectValue));
} else if (typName.equals(Constants.JAVA_LANG_SHORT)) {
method.invoke(object, Short.parseShort(objectValue));
} else if (typName.equals(Constants.JAVA_LANG_FLOAT)) {
method.invoke(object, Float.valueOf(objectValue));
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
|