springmvc 간단한 파일 업로드 + 상품은 UI를 제공.

/ ** 
 * 추가 제품은 
 * 스트림 파일의 방법으로 업로드 
 * 
 * @param 
 * @return 
 얻은 파일 CommonsMultipartFile 객체로 패키지 이름을 제어 할 수 * @RequestParam ( "파일") = 파일 
 * / 
@RequestMapping (값 = "/를 addProduct "방법 = RequestMethod.POST) 
공공 문자열 addProduct (@RequestParam ("이름 ") 문자열 이름, @RequestParam ("설명 ") 문자열 설명, 
                         @RequestParam (". 가격 ")을 두 번. 가격, @RequestParam ("주식 ") 정수 증권, 
                         @RequestParam (이하 "parentId") 문자열 parentId, 
                         @RequestParam ( "fileName에")의 MultipartFile 파일, 
                         @RequestParam ( "내림차순") 문자열 내림차순)는 IOException가 {던졌습니다 
    분할의 // 값을
    에서 System.out.println합니다 (parentId) 
    문자열 [] P = parentId.split ( "-"); 
    있는 Integer.parseInt P1에서 INT = (P [0]); 
    있는 Integer.parseInt P2 = int 형 (P의 [이. 1]), 
    INT 있는 Integer.parseInt = P3 (P [2]) 
    에서 System.out.println ( "+ P2 +"P1 + ""+ P3) 
    에서 System.out.println ( "인쇄 파일 디스크립터"DESC +); 
    // JSP 파일을 업로드 : 파일 
    ()의 InputStream inputStream을 = file.getInputStream을; // 입력 스트림 IO는 
    파일 이름 업로드 // 
    문자열 fileName에 = file.getOriginalFilename를 (); 
    의 OutputStream의 OutputStream = 
            새로운 새로운 FileOutputStream에 ( "F : \\ \\ bdqn_Y2 ssm1 \ \ 이미지 웹 \\ \\ \\ 제품 "+ fileName에) 
    바이트 [] 바이트 = 새로운 새 바이트 [1024]; 
    INT 길이 = -1, 
    그동안 ((길이 = inputStream을.{) (바이트))! = -1 읽기 
        OutputStream.write (바이트 0, 길이);
    } 
    // 가까운 스트림
    outputStream.close (); 
    inputStream.close (); 
    //将파일上传到服务器,硬盘中
    에서 System.out.println ( "成功上传!"); 
    제품 제품 = 새로운 제품 (); 
    product.setCategoryLevel1Id (P1); 
    product.setCategoryLevel2Id (P2); 
    product.setCategoryLevel3Id (P3); 
    product.setName (이름); 
    product.setDescription (설명); 
    product.setPrice (가격); 
    product.setStock (주); 
    product.setFileName (파일 이름); 
    INT NUM = productService.addProduct (제품); 
    에서 System.out.println (NUM); 
    경우 (NUM> 0) { 
        "리디렉션 : index1.jsp"를 반환; 
    } 다른 {
        반환 "/ / 제품 추가 관리"; 
    } 

}
  • UI
  • <TR> 
        <TD 클래스 = "필드">商品图片(*) : </ TD> 
        <TD> <input TYPE = "파일"클래스 = "텍스트"NAME = 'FILENAME "/> 
            <BR/>文件描述: <input 타입 = "텍스트"이름 = "DESC"/> 
            <SPAN> </ SPAN> </ TD> 
    </ TR>

추천

출처blog.csdn.net/qq_40585396/article/details/85247364