⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadbean.java

📁 用SPRING做的一个企业信息系统网站
💻 JAVA
字号:
package com.enterpriseweb.bean;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.util.Calendar;

import org.apache.struts.upload.FormFile;

import com.enterpriseweb.struts.form.HonourForm;
import com.enterpriseweb.struts.form.InstructorForm;
import com.enterpriseweb.struts.form.ProductForm;

public class UploadBean{

	public Date getDate() {
		// TODO Auto-generated method stub
		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 da = Date.valueOf(date);
		return da;
	}

	public String getDateWithNum() {
		// TODO Auto-generated method stub
		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 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 String getDateWithString() {
		// TODO Auto-generated method stub
		 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() {
		// TODO Auto-generated method stub
		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 getFileName(Object object, String formname) {
		// TODO Auto-generated method stub
		FormFile file = null;
		if(formname == "honourForm"){
			HonourForm honour = (HonourForm)object;
			file = honour.getImageFile();
		}
		if(formname == "instructorForm"){
			InstructorForm ins = (InstructorForm)object;
			file = ins.getImageFile();
		}
		if(formname == "productForm"){
			ProductForm pf = (ProductForm)object;
			file = pf.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 String toChinese(String s) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		return null;
	}*/

	public boolean upload(String dir, Object object, String formName)
			throws Exception {
		// TODO Auto-generated method stub
		FormFile file = null;
		String filename = null;
		if(formName == "InstructorForm"){
			InstructorForm ins = (InstructorForm)object;
			file = ins.getImageFile();
			if(file.getFileSize()>10000){
				return false;
			}
			filename = this.getFileName(ins, "InstructorForm");
			ins.setImagepath(filename);
		}
		if(formName == "ProductForm"){
			ProductForm product = (ProductForm)object;
			file = product.getImageFile();
			if(file.getFileSize()>10000){
				return false;
			}
			filename = this.getFileName(product, "ProductForm");
			product.setImagepath(filename);
		}
		if(formName == "HonourForm"){
			HonourForm honour = (HonourForm)object;
			file = honour.getImageFile();
			if(file.getFileSize()>10000){
				return false;
			}
			filename = this.getFileName(honour, "HonourForm");
			honour.setImagepath(filename);
		}
		//创建读取用户上传文件的对象
		InputStream ins = file.getInputStream();
		
		BufferedInputStream br = new BufferedInputStream(ins);
		//创建把上传数据写到目标文件的对象
		File uploadfile = new File(dir);
		if(!uploadfile.exists() || uploadfile == null){
			uploadfile.mkdirs();
		}
		OutputStream out = new FileOutputStream(uploadfile.getPath()+"/"+filename);
		System.out.println("上传目录:" + uploadfile.getPath() + "/" + filename);
		BufferedOutputStream bro = new BufferedOutputStream(out);
		int byteReads = 0;
		byte[] buffer = new byte[8192];
		while((byteReads = br.read(buffer, 0, 8192))!=-1){
			bro.write(buffer, 0, byteReads);
		}
		bro.flush();
		bro.close();
		br.close();
		ins.close();
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -