ssm框架的文件上传

File实体

public class FileEntity {
    //文件id
	private Integer id;
	//文件路径
	private String path;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	
}

JSP代码

<form action = "upload" method="post" enctype="multipart/form-data">
 	   	<input type="file" name = "file">
 	   	<input type="submit" value="上传文件"> 
</form>

Controller代码

/**
* 文件上传
 * @param file 文件对象
 * @param request
 * @return 返回地址
 */
	@RequestMapping("/upload")
	public String  upload(@RequestParam CommonsMultipartFile file,HttpServletRequest request){
		
		String filename = null ;
		try{
		    // /view/static/files放上传的文件
			//获取上下文路径
			String realPath = request.getSession().getServletContext().getRealPath("/view/static/files");
			System.out.println("realPath==>"+realPath);
			//获取文件名
			filename = file.getOriginalFilename();
			//文件类型
			filename.substring(filename.indexOf("."),filename.length());
			File files= new File(realPath, filename);
			if(!files.exists()){
				files.mkdir();
			}
			//转存文件到指定的路径
			file.transferTo(files);
			//传入路径
			staffService.uploadFile("view/static/files/"+filename);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return "system/index";
	}

=================================
以下是常规:
Service层

   public interface StaffService {
    /**
	 * 上传文件
	 */
	void uploadFile(String filename);
	}

ServiceImp层

@Service
public class FileServiceImp implements StaffService {
    @Autowired
	private FileDao fileDao;
	public void uploadFile(String filePath) {
		// TODO Auto-generated method stub
		try {
			fileDao.uploadFileDao(filePath);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

dao层

public interface FileDao {
	/**
	 * 上传文件
	 * @param filePath
	 */
	void uploadFileDao(String filePath)throws Exception;
}

fileMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper namespace="com.srm.system.dao.FileDao">
 <resultMap type="FileEntity" id="idfileEntity" autoMapping="true">
  		<id column="id" property="id"/>
  		<result column="path" property="path"/>
</resultMap>
	
  	<!-- 上传文件 -->
  	<insert id="uploadFileDao">
  		insert  into file (path) values(#{path})
  	</insert>
  </mapper>

==================================================
总结
单文件上传
方法一

	@RequestMapping("/addFileMessage")
	public String addFileMessage(Model model,FileEntity fileEntity,MultipartFile file,HttpServletRequest request){
		try {
			//获取文件上传保存地址
			String realPath = request.getSession().getServletContext().getRealPath("/view/static/files");
			System.out.println("realPath==>"+realPath);
			String filename = file.getOriginalFilename();
			//获取后缀名
			filename.substring(filename.indexOf("."),filename.length());
			 //文件名字
			fileEntity.setName(filename);
			
			//设置路径
			fileEntity.setPath("view/static/files/"+filename);
			//上传
			fileService.addFileMessage(fileEntity);
			
			File cuttentfile= new File(realPath, filename);
			if(!cuttentfile.exists()){
				cuttentfile.mkdir();
			}
			//转存文件到指定的路径
			file.transferTo(cuttentfile);		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";

	}

方法二

@RequestMapping("/addFileMessage")
	public String addFileMessage(Model model,FileEntity fileEntity,	MultipartHttpServletRequest request){
		try {
			MultipartFile file =   request.getFile("uploadFile");
			String filename = file.getOriginalFilename();
			InputStream isRef = file.getInputStream();
			//获取文件上传保存地址
			String realPath = request.getSession().getServletContext().getRealPath("/view/static/files");
			System.out.println("realPath==>"+realPath);
			 //文件名字
			fileEntity.setName(filename);
			//设置路径
			fileEntity.setPath("view/static/files/"+filename);
			//上传
			fileService.addFileMessage(fileEntity);
			
			File cuttentfile= new File(realPath, filename);
			FileOutputStream fosRef = new FileOutputStream( cuttentfile);
			IOUtils.copy(isRef, fosRef);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

多文件上传

@Controller
public class FileUpload
@RequestMapping ("/moreFileUpload1")
public String oneFileUpload
(MultipartHttpServletRequest request,HttpServletResponse response){
try {
	System.out.printIn ("username=" +request.getParameter ("username")) ;
	Map<String,MultipartFile> flleMap = request.getFileMap();
	System.out.println("文件个数为: " + fileMap.size());
	Set<String> fileset = fileMap.keySet();
	Iterator<string> fileNameIterator = fileset.iterator();
	while (fileNameIterator.hasNext() (
		String uploadFileName = fileNameIterator.next();
		System. out.println (uploadFileName);
		MultipartFile file = fileMap.get (uploadFileName) ;
		uploadFileName = file.getOriginalFilename() ;
		InputStream isRef = file.getInputStream() ;
		String targetDir = request.getSession().getServletContext().getRealPath ("/upload") ;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_ MM dd_ hh_ mm_ ss") ;
		String getDateString = sdf. format (new Date());
		File targetFile = new File (targetDir,””+ getDateString +System.nanoTime() + "_'
		uploadFileName) ;
		FileOutputStream fosRef = new FileOutputStream (targetFile);
		Ioutils.copy (isRef, fosRef);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值