首页>代码>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; } }
最近下载更多