?? specialmethod.java
字號:
package mrgf.other;
import java.io.*;
import java.util.Calendar;
import java.sql.Date;
import org.apache.struts.upload.FormFile;
import mrgf.form.ManageMerchandiseForm;
public class SpecialMethod {
public SpecialMethod() {
}
//解決中文亂碼
public String toChinese(String s) {
String result = null;
try {
result = new String(s.trim().getBytes("ISO8859_1"), "gb2312");
} catch (UnsupportedEncodingException ex) {
System.out.println("字符轉(zhuǎn)碼錯誤!!!");
ex.printStackTrace();
}
return result;
}
//上傳文件
public void upload(String dir, ManageMerchandiseForm form) throws Exception {
// 創(chuàng)建欲上傳文件的對象
FormFile file = form.getMerchandisePhoto();
// 取系統(tǒng)時間并進(jìn)行格式化
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
String date = year + "";
if (month < 10) {
date = date + "0" + month;
} else {
date = date + month;
}
if (day < 10) {
date = date + "0" + day;
} else {
date = date + day;
}
if (hour < 10) {
date = date + "0" + hour;
} else {
date = date + hour;
}
if (minute < 10) {
date = date + "0" + minute;
} else {
date = date + minute;
}
if (second < 10) {
date = date + "0" + second;
} else {
date = date + second;
}
// 取欲上傳的文件的名字和長度
String fileName = file.getFileName();
int fileSize = file.getFileSize();
// 將文件上傳時間加入文件名,避免發(fā)生覆蓋同名文件現(xiàn)象
int i = fileName.indexOf(".");
String name = "[" + date + "]";
String type = fileName.substring(i + 1);
fileName = name + "." + type;
// 修改form中的文件名及文件大小
form.setMerchandisePhotoName(fileName);
form.setMerchandisePhotoSize(fileSize);
// 創(chuàng)建讀取用戶上傳文件的對象
InputStream streamIn = file.getInputStream();
// 創(chuàng)建把上傳數(shù)據(jù)寫到目標(biāo)文件的對象
File uploadFile = new File(dir);
// 判斷指定路徑是否存在,不存在則創(chuàng)建路徑
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
OutputStream streamOut = new FileOutputStream(uploadFile.getPath() +
"/" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
file.destroy();
}
//取系統(tǒng)日期
public String getDateWithString() {
Calendar now = Calendar.getInstance();
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String result = year + " 年 " + month + " 月 " + day + " 日 ";
return result;
}
//取系統(tǒng)日期
public String getDateWithYMD() {
Calendar now = Calendar.getInstance();
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String date = year + "-" + month + "-" + day;
return date;
}
//取系統(tǒng)日期
public String getDateWithNum() {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
String smonth = "" + month;
String sday = "" + day;
if (month < 10) {
smonth = "0" + month;
}
if (day < 10) {
sday = "0" + day;
}
String date = year + smonth + sday;
return date;
}
//取系統(tǒng)日期
public Date getDate() {
Calendar now = Calendar.getInstance();
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String date = year + "-" + month + "-" + day;
Date result = Date.valueOf(date);
return result;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -