图片文件上传及其它操作

来源:互联网 发布:极致游戏优化版win7 编辑:程序博客网 时间:2024/06/10 01:35

1、保存图片并获取路径

/** * 获取图片路径信息 *  * @author zcq * @param picName * @param suffix * @return */private String getPicPath(String picName, String suffix) {    String picPath = "resource" + File.separator + "upload" + File.separator + "headImage" + File.separator            + picName + suffix;    File newFile = new File(request.getServletContext().getRealPath(File.separator) + picPath);    if (newFile.exists()) {        picPath = getHeadPic(picName + CreateRandomCode.getRandomCode(2), suffix);    }    return picPath;}
2、移动图片或重命名

/** * 移动或重命名图片 *  * @author zcq * @param from * @param dest */private void movePic(String from, String dest) {    String temp_path = request.getServletContext().getRealPath(File.separator);    // 创建文件夹    File destFile = new File(temp_path + dest.substring(0, dest.lastIndexOf(File.separator)));    destFile.mkdirs();    new File(from).renameTo(new File(temp_path + dest));// 移动图片到指定文件夹}
3、创建文件夹

/** * 创建机构文件夹 *  * @author zcq * @param insId */private void createInsFile(String insId) {    String temp_path = request.getServletContext().getRealPath(File.separator);    String newFilePath = temp_path + "logo" + File.separator + insId;    // 建立对应的目录    new File(newFilePath).mkdir();    String oldPatht = temp_path + "template" + File.separator + "封面logo.png";    String oldPathb = temp_path + "template" + File.separator + "封底logo.png";    String destb = newFilePath + File.separator + "封面logo.png";    String destt = newFilePath + File.separator + "封底logo.png";    copy(oldPatht, destt);    copy(oldPathb, destb);}

4、复制图片

/** * 复制图片 *  * @author zcq * @param from * @param dest */private void copy(String from, String dest) {    // 建立对应的目录    new File(dest.substring(0, dest.lastIndexOf("/"))).mkdirs();    FileInputStream fromFS = null;    FileChannel fromFC = null;    FileOutputStream destFS = null;    FileChannel destFC = null;    try {        fromFS = new FileInputStream(from);        destFS = new FileOutputStream(dest);        destFC = destFS.getChannel();        fromFC = fromFS.getChannel();        fromFC.transferTo(0, fromFC.size(), destFC);    } catch (IOException e) {        e.printStackTrace();    } finally {        try {            fromFC.close();            fromFS.close();            destFC.close();            destFS.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

5、删除文件

/** * 删除文件夹中的所有内容而不删除文件夹本身 *  * @author zcq * @param path * @return flag */public static boolean delAllFile(String path) {    boolean flag = false;    File file = new File(path);    if (!file.exists()) {        return flag;    }    if (!file.isDirectory()) {        return flag;    }    String[] tempList = file.list();    File temp = null;    for (int i = 0; i < tempList.length; i++) {        if (path.endsWith(File.separator)) {            temp = new File(path + tempList[i]);        } else {            temp = new File(path + File.separator + tempList[i]);        }        if (temp.isFile()) {            temp.delete();        }        if (temp.isDirectory()) {            delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件            temp.delete();// 再删除文件夹本身            flag = true;        }    }    return flag;}

6、上传图片FORM表单

/** * 上传Form表单 *  * @author zcq * @version 1.0 *  *          变更履历:v1.0 2015-2-4 zcq 初版 */public class UploadForm {    /** 上传事件ID,主要区分属于哪部分业务使用 */    private String upid;    /** 当上传多个图片,此参数有用,代表第几张图片 */    private String picsheet;    /** 文件列表 */    private MultipartFile Filedata;    /** 临时上传路径 */    private String picType;    public String getUpid() {        return upid;    }    public void setUpid(String upid) {        this.upid = upid;    }    public String getPicsheet() {        return picsheet;    }    public void setPicsheet(String picsheet) {        this.picsheet = picsheet;    }    public MultipartFile getFiledata() {        return Filedata;    }    public void setFiledata(MultipartFile filedata) {        Filedata = filedata;    }    public String getPicType() {        return picType;    }    public void setPicType(String picType) {        this.picType = picType;    }}
7、上传图片Controller

/** * 通用上传 *  * @author zcq * @param request * @param response * @param uploadForm * @return */@ResponseBody@RequestMapping(value = "upload.action")public FileUploadResult upload_image(HttpServletRequest request, HttpServletResponse response, UploadForm uploadForm) {    // 图片上传    FileUploadResult result = new FileUploadResult();    String tmp_path = "";    String picName = "";    String picType = uploadForm.getPicType();    // 食材图片上传临时路径    if ("foodExtPic".equals(picType)) {        tmp_path = "resource" + File.separator + "upload" + File.separator + "food_temp" + File.separator + "ext";    }    // 菜谱图片上传临时路径    if ("foodCuisinePic".equals(picType)) {        tmp_path = "resource" + File.separator + "upload" + File.separator + "food_temp" + File.separator                + "cuisine";    }    MultipartFile patch = uploadForm.getFiledata();    if (patch != null && !patch.isEmpty()) {        String fileName = patch.getOriginalFilename();// 文件名,不包含路径        String suffix = fileName.substring(fileName.lastIndexOf("."));// 取扩展名        File filePath = new File(request.getSession().getServletContext().getRealPath(tmp_path));        File newFile = new File(filePath.getAbsolutePath());// 文件存储路径        if (!newFile.exists()) {            newFile.mkdirs();        }        try {            String newFileName = "";            if (StringUtils.isEmpty(picName)) {                Random random = new Random(9999); // 随机数                newFileName = JodaTimeTools.getCurrentDate(JodaTimeTools.FORMAT_8) + random.nextInt(9999) + suffix;            } else {                newFileName = picName + suffix;            }            patch.transferTo(new File(filePath.getAbsolutePath() + File.separator + newFileName));            result.setResult(true);            if (tmp_path.endsWith(File.separator)) {                result.setUppath(tmp_path + newFileName);            } else {                result.setUppath(tmp_path + File.separator + newFileName);            }        } catch (Exception e) {            e.printStackTrace();            result.setUppath("[300]文件上传出错了!");        }        if (File.separator.equals("\\")) {            result.setUppath(result.getUppath().replaceAll("\\\\", "/"));        }    }    return result;}


0 0
原创粉丝点击