1

in case of upload1.action, there's no error BUT nothing comes out.

upload2, upload3 there's an error under.

Forwarding to error page from request [/upload2.action] due to exception [null]
java.lang.NullPointerException: null

zzController.java

public class ZzController {

@RequestMapping("/form")
public String form(){
    return "form";
}

@RequestMapping("/zz/upload1.action")
public ModelAndView upolad1(
        @RequestParam("hakbun") String num,
        @RequestParam("report") MultipartFile file){
    System.out.println(num);
    ModelAndView mav = new ModelAndView();
    String filename = file.getOriginalFilename();
    System.out.println(filename);
    //실제 파일을 업로드하기 위한 파일 객체 생성
    File f = new File("c:\\upload\\"+num+"_"+filename);
    //한번에 한해서 동일한 파일이 존재하면 이름에 (1) ,
    //(나중에)2번째에도 검사해서 이름을 편해보고, 확장자 앞에 다른 이름을 추가하도록 해보자 
    if(f.exists()){
        f = new File("c:\\upload\\"+num+"_"+filename+"(1)");
    }        

    try {
        file.transferTo(f);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mav;
}


@RequestMapping("/upload2.action")
public ModelAndView upolad2(MultipartHttpServletRequest request){
    ModelAndView mav = new ModelAndView();
    String hakbun = request.getParameter("hakbun");
    System.out.println(hakbun);
    MultipartFile f = request.getFile("report");
    String filename = f.getOriginalFilename();
    System.out.println(f);
    System.out.println(filename);
    try {
        byte[] b = f.getBytes();
        File file = new File("c:\\upload\\"+hakbun+"_"+filename);
        //System.out.println(file.lastModified());
        //System.out.println(new java.sql.Date(file.lastModified()));
        //System.out.println(new java.util.Date(file.lastModified()));
        //파일에 바이트 배열을 기록할 수 있는 스트림 생성
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(b);
        fos.close();
    } catch (IOException e){
        System.out.println(e.getMessage());
    }

    return mav;
}   


@RequestMapping("/upload3.action")
public ModelAndView upolad3(ZzVO zz, HttpServletRequest request, HttpSession session){
    ModelAndView mav = new ModelAndView();

    //프로젝트 내의 f 폴더의 경로를 만들기

    String path = request.getRealPath("/f");
    System.out.println(path);
    String num = zz.getHakbun();
    System.out.println(num);
    String filename =zz.getReport().getOriginalFilename();
    System.out.println(filename);


    try {
        zz.getReport().transferTo(new File(path+"/"+num+"_"+filename));
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mav;
}

zzVO.java

public String getHakbun() {
    return hakbun;
}
public void setHakbun(String hakbun) {
    this.hakbun = hakbun;
}
public MultipartFile getReport() {
    return report;
}
public void setReport(MultipartFile report) {
    this.report = report;
}
@Override
public String toString() {
    return "Command [hakbun=" + hakbun + ", report=" + report + "]";
}

zzApplication.java

  @Bean
MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setMaxFileSize("5120MB");
    factory.setMaxRequestSize("5120MB");
    return factory.createMultipartConfig();
}

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver=new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>@RequestParam 사용</h2>
<form action="/zz/upload1.action" method="post"
    enctype="multipart/form-data">
    <br /> 학번 : <input type="text" name="hakbun" /><br /> 파일 : <input
        type="file" name="report" /><br /> <input type="submit" value="제출"><br />
</form>

<h2>@MultipartRequest 사용!!</h2>
<form action="/zz/upload2.action" method="post"
    enctype="multipart/form-data">
    학번 : <input type="text" name="hakbun" /><br /> 파일 : <input type="file"
        name="report" /><br /> <input type="submit" value="제출"><br />
</form>


<h2>Command 사용</h2>
<form action="/zz/upload3.action" method="post"
    enctype="multipart/form-data">
    학번 : <input type="text" name="hakbun" /><br /> 파일 : <input type="file"
        name="report" /><br /> <input type="submit" value="제출"><br />
</form>
</body>
</html>
Lee
  • 11
  • 1

0 Answers0