首页>代码>java实现文件上传下载功能的代码实例>/springmvc-file/src/main/java/com/javaniu/controller/FileController.java
package com.javaniu.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.javaniu.dao.FileDao;
import com.javaniu.dao.UserDao;
import com.javaniu.module.File;
import com.javaniu.module.User;

@Controller
@RequestMapping({ "/file" })
public class FileController {

	Logger logger = Logger.getLogger(FileController.class);

	@Autowired
	public HttpSession session;
	@Autowired
	public UserDao userDao;
	@Autowired
	public FileDao fileDao;
	private @Value("${deploy.path}")
	String deployPath;

	@RequestMapping(value = { "" }, method = { RequestMethod.GET })
	public ModelAndView index(
			@RequestParam(value = "p", required = false, defaultValue = "1") int p) {
		ModelAndView modelAndView = new ModelAndView("file/index");
		int ps = 2;
		List<File> files = fileDao.loadAll(p, ps);
		modelAndView.addObject("files", files);
		int total = fileDao.countAll();
		int pageTotal = (total / ps) + (total % ps > 0 ? 1 : 0);
		modelAndView.addObject("pageTotal", pageTotal);
		modelAndView.addObject("p", p);
		return modelAndView;
	}

	@RequestMapping(value = { "create" }, method = { RequestMethod.GET })
	public ModelAndView create(HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView("file/create");
		User user = (User) session.getAttribute("sessionUser");
		if (user == null) {
			response.sendRedirect("/user/login.action");
			return null;
		}
		File file = new File();
		modelAndView.addObject("file", file);
		return modelAndView;
	}

	@RequestMapping(value = { "create" }, method = { RequestMethod.POST })
	public void create(@ModelAttribute("file") File file,
			HttpServletResponse response) throws Exception {
		User user = (User) session.getAttribute("sessionUser");
		if (user == null) {
			response.sendRedirect("/user/login.action");
			return;
		}
		CommonsMultipartFile commonFile = file.getFile();
		java.io.File deployDir = new java.io.File(deployPath);
		if (!deployDir.exists()) {
			deployDir.mkdirs();
		}
		file.setFileName(commonFile.getOriginalFilename());
		file.setUser(user);
		file.setFileSize(commonFile.getSize());
		String dst = deployPath + commonFile.getOriginalFilename();
		commonFile.transferTo(new java.io.File(dst));
		fileDao.save(file);
		response.sendRedirect("/file.action");
	}
}
最近下载更多
1358849392  LV21 2024年4月12日
hohhi  LV6 2022年6月5日
15185836794  LV2 2021年11月8日
EAST_Ace  LV4 2021年11月3日
naiheqingshen  LV1 2020年6月18日
yk123456321  LV1 2020年5月6日
死了还要爱  LV19 2020年3月31日
yqd123456  LV3 2020年3月23日
李东曙  LV14 2020年3月16日
Immortal code  LV1 2019年11月21日
最近浏览更多
1358849392  LV21 2024年4月12日
15103432984  LV2 2024年3月17日
潘潘123456  LV2 2023年12月24日
uni-code_0123  LV1 2023年11月29日
一只小扒菜啊 2023年10月30日
暂无贡献等级
liuind 2023年3月17日
暂无贡献等级
小白jmhuang  LV12 2023年3月10日
wyxjusj  LV1 2022年12月5日
Zyj0327 2022年11月30日
暂无贡献等级
lcy123ww  LV5 2022年11月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友