- package com.jadyer.controller;
-
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.apache.commons.io.FileUtils;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- 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.MultipartFile;
-
- import com.jadyer.model.User;
-
-
-
-
-
-
-
-
-
-
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
- private final static Map<String,User> users = new HashMap<String,User>();
-
-
- public UserController(){
- users.put("張起靈", new User("張起靈", "悶油瓶", "02200059", "menyouping@yeah.net"));
- users.put("李尋歡", new User("李尋歡", "李探花", "08866659", "lixunhuan@gulong.cn"));
- users.put("拓拔野", new User("拓拔野", "搜神記", "05577759", "tuobaye@manhuang.cc"));
- users.put("孫悟空", new User("孫悟空", "美猴王", "03311159", "sunhouzi@xiyouji.zh"));
- }
-
- @RequestMapping("/list")
- public String list(Model model){
- model.addAttribute("users", users);
- return "user/list";
- }
-
- @RequestMapping(value="/add", method=RequestMethod.GET)
- public String addUser(){
- return "user/add";
- }
-
- @RequestMapping(value="/add", method=RequestMethod.POST)
- public String addUser(User user, @RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException
- {
-
-
-
- for(MultipartFile myfile : myfiles){
- if(myfile.isEmpty()){
- System.out.println("文件未上傳");
- }else{
- System.out.println("文件長度: " + myfile.getSize());
- System.out.println("文件類型: " + myfile.getContentType());
- System.out.println("文件名稱: " + myfile.getName());
- System.out.println("文件原名: " + myfile.getOriginalFilename());
- System.out.println("========================================");
-
- String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
-
- FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
- }
- }
- users.put(user.getUsername(), user);
- return "redirect:/user/list";
- }
- }
|