首页>代码>java实现的url地址伪静态重写>/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;
	}
}
最近下载更多
gwt爱学习  LV16 2018年12月28日
huyu19921010  LV2 2018年11月6日
剑语诗情  LV2 2017年12月27日
messi10  LV25 2015年12月31日
流浪者  LV29 2015年9月5日
mengchang  LV13 2015年8月13日
sdbrk  LV14 2015年5月7日
shineer  LV7 2015年4月20日
响尾蛇  LV15 2015年4月20日
长眠在山下的人  LV20 2015年2月12日
最近浏览更多
skd666  LV8 2021年11月21日
yc19970424  LV4 2020年6月16日
jinxiao880  LV7 2020年6月8日
cpla1998  LV8 2020年5月21日
yuyiyuyi  LV6 2020年4月1日
夜聆秋雨888  LV1 2019年10月25日
花花说得对 2019年9月15日
暂无贡献等级
876888775 2019年7月30日
暂无贡献等级
Taodaahu  LV2 2019年6月12日
种花家  LV9 2019年1月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友