📄 uploadbean.java
字号:
package com.dao;
import java.io.*;
import java.util.Calendar;
import java.util.Map;
import java.sql.Date;
import java.util.HashMap;
import org.apache.struts.upload.FormFile;
import com.form.HonourForm;
import com.form.InstructorForm;
import com.form.ProductForm;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
public class UploadBean {
public String toChinese(String s) {
String result = null;
try {
result = new String(s.trim().getBytes("ISO8859_1"), "GB2312");
} catch (UnsupportedEncodingException ex) {
System.out.println("字符转码错误!!!");
ex.printStackTrace();
}
return result;
}
public String getFileName(Object object,String formname){
// 取欲上传的文件
FormFile file = null;
if (formname == "ProductForm"){
ProductForm form = (ProductForm)object;
file = form.getImageFile();
}
if(formname == "honourForm"){
HonourForm form = (HonourForm)object;
file = form.getImageFile();
}
if(formname == "instructorForm"){
InstructorForm form = (InstructorForm)object;
file = form.getImageFile();
}
// 取系统时间
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 fname = file.getFileName();
System.out.println("----------- 您上传的图片名称是:"+fname);
//int size = file.getFileSize();
//将上传时间加入文件名
int i = fname.indexOf(".");
String name = fname.substring(0, i) + "[" + date + "]";
String type = fname.substring(i + 1);
fname = name + "." + type;
System.out.println("----------- 您上传的图片名称是:" + fname);
return fname;
}
//上传文件
public boolean upload(String dir, Object object,String formName) throws Exception {
FormFile file = null;
String fname = null;
if (formName =="productForm") {
ProductForm form = (ProductForm)object;
file = form.getImageFile();
if (file.getFileSize()>100000){
return false;
}
fname = this.getFileName(form,"ProductForm");//上传的文件
form.setImagepath(fname);
}
if(formName == "honourForm"){
HonourForm form = (HonourForm)object;
file = form.getImageFile();
if (file.getFileSize()>100000){
return false;
}
fname = this.getFileName(form,"honourForm");//上传的文件
form.setImagepath(fname);
}
if(formName == "instructorForm"){
InstructorForm form = (InstructorForm)object;
file = form.getImageFile();
if (file.getFileSize()>100000){
return false;
}
fname = this.getFileName(form, "instructorForm");
form.setImagepath(fname);
}
//form.setMerchandisePhotoSize(size);
//创建读取用户上传文件的对象
InputStream streamIn = file.getInputStream();
BufferedInputStream bufferStreamIn = new BufferedInputStream(streamIn);
//创建把上传数据写到目标文件的对象
File uploadFile = new File(dir);
if (!uploadFile.exists() || uploadFile == null){
uploadFile.mkdirs();
}
OutputStream streamOut = new FileOutputStream(uploadFile.getPath() + "/" + fname);
System.out.println("上传目录:" + uploadFile.getPath() + "/" + fname);
BufferedOutputStream bufferStreamOut = new BufferedOutputStream(streamOut);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bufferStreamIn.read(buffer, 0, 8192)) != -1) {
bufferStreamOut.write(buffer, 0, bytesRead);
}
bufferStreamOut.flush();
bufferStreamOut.close();
bufferStreamIn.close();
file.destroy();
return true;
}
//取系统日期
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;
}
//取系统日期
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;
}
//取系统日期
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;
}
//取系统日期
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -