首先多文件上傳當出現action找不到的時候必定要查看本身的表單的提交方式,enctype方式,默認不支持文件上傳。java
後臺代碼的處理方式以下所示ajax
/**
* 發佈活動
*/
public void Activity_reply() {
String date = TimeUtils.getCurrentTime();
activity_releaseService.applyActivity(activity_release,date);
//若是的到的上傳文件爲空,則不處理
if(file!=null){
for(int i=0;i<file.size();i++){
//循環上傳每個文件
upload(i,activity_release.getActivity_email(),date);
}
}
}算法
/**
* 處理上傳的圖片
* @param i
* @param email
* @param date
* @return
*/
public String upload(int i,String email,String date) {
String filename=this.getFileFileName().get(i);
String fileType=this.getFileContentType().get(i);
String path="E:\\Tomcat\\uploads\\";
filename=UploadUtils.getFileName(filename);
String taskfilePath=path+filename;
File file_path=new File(taskfilePath);
String result=null;
return result=activity_releaseService.upload_image(file.get(i),file_path,taskfilePath,email,date);
}json
@Override
public String upload_image(File file, File file_path, String taskfilePath,
String email,String date) {
// TODO Auto-generated method stub
try {
FileUtil.copyFile(file, file_path);
FilePath filePath=new FilePath();
filePath.setEmail(email);
filePath.setFilepath(taskfilePath);
filePath.setDate(date);
activity_releaseDao.upload_image(filePath);
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
}session
時間工具類app
package Vms.Lee;dom
import java.text.SimpleDateFormat;
import java.util.Date;ide
import org.junit.Test;工具
public class TimeUtils {
public static String getCurrentTime(){
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateCurrent=dateFormat.format(new Date());
return dateCurrent;
}
public static String getCurrentTimeNumber(){
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
String dateCurrent=simpleDateFormat.format(new Date());
return dateCurrent;
}
}ui
MD5Utils加密工具類
package Vms.Lee;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
/**
* 使用md5的算法進行加密
*/
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("沒有md5這個算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);// 16進制數字
// 若是生成數字未滿32位,須要前面補0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
文件名工具類
package Vms.Lee;
import java.util.Random;
import java.util.UUID;
import org.junit.Test;
public class UploadUtils {
public static String getFileName(String filename){
// 先查找
int index = filename.lastIndexOf(".");
// 截取
String lastname = filename.substring(index, filename.length());
// 惟一 字符串 fsd-sfsdf-sfsd-sdfsd
String date=TimeUtils.getCurrentTimeNumber();
Random random=new Random();
date=date+random.nextInt(10);
return date+lastname;
}
public static String getUUIDName(String filename){
// 先查找
int index = filename.lastIndexOf(".");
// 截取
String lastname = filename.substring(index, filename.length());
// 惟一 字符串 fsd-sfsdf-sfsd-sdfsd
String uuid = UUID.randomUUID().toString().replace("-", "");
return uuid+lastname;
}
@Test
public void test() {
String filename = "girl.jpg";
String uuid = getFileName(filename);
System.out.println(uuid);
}
}
hibernate加載工具類
package Vms.Lee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* hibernate開始session
* @author LesLee
*
*/
public class HibernateUtils {
private static final Configuration CONFIG;
private static final SessionFactory FACTORY;
// 編寫靜態代碼塊
static{
// 加載XML的配置文件
CONFIG = new Configuration().configure();
// 構造工廠
FACTORY = CONFIG.buildSessionFactory();
}
public static Session getsSession(){
return FACTORY.openSession();
}
}
fastjson工具類
package Vms.Lee;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class FastJsonUtils {
/**
* 將對象轉成json串
* @param object
* @return
*/
public static String toJSONString(Object object){
//DisableCircularReferenceDetect來禁止循環引用檢測
return JSON.toJSONString(object,SerializerFeature.DisableCircularReferenceDetect);
}
//輸出json
public static void write_json(HttpServletResponse response,String jsonString){
response.setContentType("application/json;utf-8");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* ajax提交後回調的json字符串
* @return
*/
public static String ajaxResult(boolean success,String message)
{
Map map=new HashMap();
map.put("success", success);//是否成功
map.put("message", message);//文本消息
String json= JSON.toJSONString(map);
return json;
}
/**
* JSON串自動加前綴
* @param json 原json字符串
* @param prefix 前綴
* @return 加前綴後的字符串
*/
public static String JsonFormatterAddPrefix(String json,String prefix,Map<String,Object> newmap)
{
if(newmap == null){
newmap = new HashMap();
}
Map<String,Object> map = (Map) JSON.parse(json);
for(String key:map.keySet()) { Object object=map.get(key); if(isEntity(object)){ String jsonString = JSON.toJSONString(object); JsonFormatterAddPrefix(jsonString,prefix+key+".",newmap); }else{ newmap.put(prefix+key, object); } } return JSON.toJSONString(newmap); } /** * 判斷某對象是否是實體 * @param object * @return */ private static boolean isEntity(Object object) { if(object instanceof String ) { return false; } if(object instanceof Integer ) { return false; } if(object instanceof Long ) { return false; } if(object instanceof java.math.BigDecimal ) { return false; } if(object instanceof Date ) { return false; } if(object instanceof java.util.Collection ) { return false; } return true; }}