首页>代码>SSM图书馆后台管理系统,带前后台>/src/com/bysj/controller/BaseController.java
package com.bysj.controller;

import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.bysj.utils.UserException;

public class BaseController {
	
	
	
	@InitBinder
	// 必须有一个参数WebDataBinder
	public void initBinder(WebDataBinder binder) {
		//System.out.println(binder.getFieldDefaultPrefix());
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd"), false));
	 
		binder.registerCustomEditor(Integer.class, new PropertyEditorSupport() {
			@Override
			public String getAsText() { 
				return (getValue() == null) ? "" : getValue().toString();
			} 
			@Override
			public void setAsText(String text) {
				Integer value = null;
				if (null != text && !text.equals("")) {  
						try {
							value = Integer.valueOf(text);
						} catch(Exception ex)  { 
							throw new UserException("数据格式输入不正确!"); 
						}  
				}
				setValue(value);
			} 
		});
	  
		//binder.registerCustomEditor(Integer.class, null,new CustomNumberEditor(Integer.class, null, true));
		
		binder.registerCustomEditor(Float.class, new PropertyEditorSupport() {
			@Override
			public String getAsText() { 
				return (getValue() == null)? "" : getValue().toString();
			} 
			@Override
			public void setAsText(String text)  {
				Float value = null;
				if (null != text && !text.equals("")) {
					try {
						value = Float.valueOf(text);
					} catch (Exception e) { 
						throw new UserException("数据格式输入不正确!"); 
					}
				}
				setValue(value);
			}
		});
	}
 
   
	/**
	 * 处理图片文件上传,返回保存的文件名路径
	 * fileKeyName: 图片上传表单key
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	public String handlePhotoFileUpload(HttpServletRequest request,String fileKeyName) throws IllegalStateException, IOException {
		String fileName = "upload/NoImage.jpg";
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        /**构建图片保存的目录**/
        String photoBookPathDir = "/upload";
        /**得到图片保存目录的真实路径**/
        String photoBookRealPathDir = request.getSession().getServletContext().getRealPath(photoBookPathDir);
        /**根据真实路径创建目录**/
        File photoBookSaveFile = new File(photoBookRealPathDir);
        if(!photoBookSaveFile.exists())
        	photoBookSaveFile.mkdirs();
        /**页面控件的文件流**/
        MultipartFile multipartFile_photoBook = multipartRequest.getFile(fileKeyName);
        if(!multipartFile_photoBook.isEmpty()) {
        	/**获取文件的后缀**/
            String suffix = multipartFile_photoBook.getOriginalFilename().substring
                            (multipartFile_photoBook.getOriginalFilename().lastIndexOf("."));
            /**使用UUID生成文件名称**/
            String photoBookFileName = UUID.randomUUID().toString()+ suffix;//构建文件名称
            //String logImageName = multipartFile.getOriginalFilename();
            /**拼成完整的文件保存路径加文件**/
            String photoBookFilePath = photoBookRealPathDir + File.separator  + photoBookFileName;
            File photoBookFile = new File(photoBookFilePath);
            multipartFile_photoBook.transferTo(photoBookFile);
            fileName = "upload/" + photoBookFileName;
        }
		return fileName;
	}
 

}
最近下载更多
星予宝藏666  LV6 2023年6月7日
hwz050059  LV1 2023年5月30日
zcx12345678  LV6 2023年5月29日
不足挂齿  LV2 2023年2月1日
and123456  LV11 2022年7月15日
stdtta  LV8 2022年6月28日
huyyyyy  LV8 2021年12月24日
1727779658  LV7 2021年12月20日
1178995129  LV6 2021年12月12日
wanglinddad  LV54 2021年12月7日
最近浏览更多
pangzhihui  LV12 4月11日
2131234536546  LV7 3月31日
1437047954 3月7日
暂无贡献等级
xwh2000  LV2 3月5日
duoduo1234  LV2 1月15日
XiaoSong888  LV2 1月10日
Camellia-while  LV1 1月2日
xiaomii  LV3 2023年12月15日
limenghaoqwer  LV2 2023年11月27日
wwewww  LV4 2023年11月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友