首页>代码>spring mvc 结合 urlrewrite实现伪静态化的demo实例>/springmvc-urlrewrite/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.PathVariable;
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");
	}

	@RequestMapping(value = { "{id}" }, method = { RequestMethod.GET })
	public ModelAndView file(@PathVariable("id") Long id,
			HttpServletResponse response) throws IOException {
		ModelAndView modelAndView = new ModelAndView("file/file");
		File file = fileDao.findById(id);
		modelAndView.addObject("file", file);
		return modelAndView;
	}
}
最近下载更多
vhjjvj  LV2 2020年12月15日
itezra  LV2 2019年9月2日
987009736  LV1 2018年9月6日
provencesl  LV12 2018年6月29日
【天天向上】  LV2 2018年5月28日
dj_123  LV16 2018年5月23日
剑语诗情  LV2 2017年12月27日
villorshtz  LV2 2017年12月10日
zhuziyu  LV1 2017年12月4日
java前进中  LV3 2017年11月7日
最近浏览更多
xk9587  LV16 2月7日
lihuazone  LV7 2021年6月10日
秦sir3067683450  LV10 2021年6月2日
xb1406112453  LV5 2021年3月31日
vhjjvj  LV2 2020年12月15日
ghy4629 2020年11月11日
暂无贡献等级
peter_ou 2020年7月17日
暂无贡献等级
no one and you  LV2 2020年6月8日
18697181716 2020年6月3日
暂无贡献等级
15088605151@139.com  LV12 2020年5月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友