주간 베스트 월간 베스트 3개월 베스트 베스트 게시물
연길시 신화서점 조선말 도서

spring + fileupload

hmily1129 | 2013.08.05 15:24:58 댓글: 0 조회: 2149 추천: 0
분류웹 프로그래밍 https://life.moyiza.kr/itstudy/2001788

톰캣에서 아래 두 파일은 다운받아서 프로젝트에 추가한다.
commons-fileupload-1.3.jar
commons-io-2.4.jar


1. web.xml 설정은 생략
dispatchservlet를 설정하여 url형식으로 된것만 통과되게 하는놈.

2. beans 설정
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <context:component-scan base-package="com.eLab.Services" />
 <mvc:annotation-driven />
 <bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 </bean>
</beans>

설명 : 자동 주석 해석, 파일 컨트롤의 해석모듈을 추가한다.

@Controller
public class *Controllername{
@RequestMapping(value = "/form", method = RequestMethod.POST)
 public @ResponseBody
 String handleFormUpload(@RequestParam("name") String name,
   @RequestParam("file") MultipartFile file) throws IOException {
  InputStream inputStream = file.getInputStream();
  File outFile = new File("123.jpg");
  System.out.print(outFile.getAbsolutePath());
  OutputStream outStream = new FileOutputStream(outFile);
  byte[] buf = new byte[1024];
  int len = 0;
  while ((len = inputStream.read(buf)) > 0) {
   outStream.write(buf, 0, len);
  }
  outStream.close();
  inputStream.close();
  return "dd";

 }

설명 : 파일명과 패일을 받아서 처리한다.

3.jQuery Form Plugin
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<script src="Script/jquery-1.9.1.js"></script>
<script src="Script/jquery.form.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
 jQuery(function($) {
  $('#ajaxform1').ajaxForm({
   success : function(msg) {
    alert(msg);
   }
  });
 });

 function uploadfile() {
  $('#uploadfile').trigger('click');
 }
</script>
</head>
<body>
 <h1>Please upload a file</h1>
 <form id="ajaxform1" method="post" action="form.do"
  enctype="multipart/form-data">
  <input type="text" name="name" /> <input type="file" name="file"
   id="file_upload" /> <input type="submit" id="uploadfile" />
 </form>
 <input type="button" value="submit" onclick="uploadfile()" />
</body>
</html>

설명 : 기본 form 호출을 script로 해준다.

추천 (0) 선물 (0명)
IP: ♡.118.♡.4
3,006 개의 글이 있습니다.
제목 글쓴이 날짜 조회
관리자
2003-09-20
11659
관리자
2003-09-20
11210
관리자
2003-09-20
20253
지구인
2010-08-27
19104
지구인
2009-09-07
13397
SOLIDH
2010-01-29
15414
엔죠라이프
2004-10-07
16518
CHOSUN
2014-01-11
2436
CHOSUN
2014-01-07
2595
CHOSUN
2014-01-07
2378
CHOSUN
2014-01-07
2047
CHOSUN
2014-01-07
1789
CHOSUN
2014-01-07
1611
CHOSUN
2014-01-07
1176
즐거운개굴
2013-09-29
3001
hmily1129
2013-09-03
4909
hmily1129
2013-08-29
5361
hmily1129
2013-08-26
4792
hmily1129
2013-08-25
3634
hmily1129
2013-08-24
4162
hmily1129
2013-08-23
4320
hmily1129
2013-08-23
3592
hmily1129
2013-08-22
1886
hmily1129
2013-08-10
1622
hmily1129
2013-08-05
2149
hmily1129
2013-08-05
2089
hmily1129
2013-07-30
1462
CHOSUN
2013-07-23
1941
모이자 모바일