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

📄 specialmethod.java

📁 &#61553 网站前台 商品销售排行
💻 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("字符转码错误!!!");
            ex.printStackTrace();
        }
        return result;
    }

    //上传文件
    public void upload(String dir, ManageMerchandiseForm form) throws Exception {
        // 创建欲上传文件的对象
        FormFile file = form.getMerchandisePhoto();
        // 取系统时间并进行格式化
        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();
        // 将文件上传时间加入文件名,避免发生覆盖同名文件现象
        int i = fileName.indexOf(".");
        String name = "[" + date + "]";
        String type = fileName.substring(i + 1);
        fileName = name + "." + type;
        // 修改form中的文件名及文件大小
        form.setMerchandisePhotoName(fileName);
        form.setMerchandisePhotoSize(fileSize);
        // 创建读取用户上传文件的对象
        InputStream streamIn = file.getInputStream();
        // 创建把上传数据写到目标文件的对象
        File uploadFile = new File(dir);
        // 判断指定路径是否存在,不存在则创建路径
        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();
    }

    //取系统日期
    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 + -