spring 과 my batis 프레임 워 크 에서 파일 업로드 기능 구현

5505 단어 spring자바
import java.io.File; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; =========== controller =========== @Controller @RequestMapping("/file") public class FileController extends BaseController{ // fileBean @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) @ResponseBody public FileBean uploadFile(FileBean fileBean HttpServletRequest request) throws Exception { CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession() .getServletContext()); commonsMultipartResolver.setDefaultEncoding("utf-8"); // if (commonsMultipartResolver.isMultipart(request)) {// MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator iter = multiRequest.getFileNames(); while (iter.hasNext()) { MultipartFile imageFile = multiRequest.getFile(iter.next().toString());//(String) iter.next() // , imageFile ,fileBean fileBean= fileService.addFile(request, imageFile, fileBean); } } return fileBean; } } =========== service =========== FileBean addFile(HttpServletRequest request,MultipartFile imageFile, FileBean fileBean); =========== service =========== @Override @Transactional public FileBean addFile(HttpServletRequest request, MultipartFile imageFile,FileBean fileBean) { try { String filePath = request.getSession().getServletContext().getRealPath("/static/fileAttach"); File file2 = new File(filePath); if (!file2.exists()) { file2.mkdirs(); } String name=imageFile.getOriginalFilename().substring(imageFile.getOriginalFilename().lastIndexOf(".")); String newName = BasicTool.getUUID()+name;// String path=filePath+File.separator+newName; File file1= new File(path); if(file1.exists()){ file1.delete(); } imageFile.transferTo(file1); fileBean.setF_name(imageFile.getOriginalFilename());// fileBean.setF_filepath("/static/fileAttach/" + newName); fileBean.setId(BasicTool.getUUID()); id fileBean.setF_uploadtime(new Date()); int addCount=0; addCount= fileWriteMapper.addFile(fileBean);// } catch (Exception e) { e.printStackTrace(); } return fileBean; } =========== dao =========== int addFile(FileBean fileBean); =========== dao =========== insert into t_file id, name, filepath, uploadtime, #{id}, #{name}, #{filepath}, #{uploadtime}, iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile imageFile = multiRequest.getFile(iter.next().toString());//(String) iter.next()
//파일 저장 방법, 그 중 매개 변수 imageFile 은 파일 경로, fileBean 파일 실체
fileBean= fileService.addFile(request, imageFile, fileBean);
}
}
return fileBean;
}
}
= = = = = = = = = = = 파일 업로드 서비스 층 = = = = = = = = = = = = = = = = = = = =
FileBean addFile(HttpServletRequest request,MultipartFile imageFile, FileBean fileBean);
= = = = = = = = = = = 파일 업로드 서비스 구현 층 = = = = = = = = = = = = = = = = = = = = =
@Override
@Transactional
public FileBean addFile(HttpServletRequest request, MultipartFile imageFile,FileBean fileBean) {
try {
String filePath = request.getSession().getServletContext().getRealPath("/static/fileAttach");
File file2 = new File(filePath);
if (!file2.exists()) {
file2.mkdirs();
}
String name=imageFile.getOriginalFilename().substring(imageFile.getOriginalFilename().lastIndexOf("."));
String newName = BasicTool.getUUID()+name;//
String path=filePath+File.separator+newName;
File file1= new File(path);
if(file1.exists()){
file1.delete();
}
imageFile.transferTo(file1);
fileBean. setF name (imageFile. getOriginalFilename ());//파일 이름
fileBean. setF filepath ("/static/fileAttach/"+ newName); 파일 경로
fileBean. setId (Basic Tool. getUUID ()); 파일 id
fileBean. setF uploadtime (new Date ()); 파일 업로드 시간
int addCount=0;
addCount = fileWrite Mapper. addFile (fileBean);//저장 방법
} catch (Exception e) {
e.printStackTrace();
}
return fileBean;
}
= = = = = = = = = = = 파일 업로드 dao 층 = = = = = = = = = = = = = = = = = = = = = = =
int addFile(FileBean fileBean);
= = = = = = = = = = = 파일 업로드 dao 구현 층 = = = = = = = = = = = = = = = = = = = = = = =
insert into t_file
id,
name,
filepath,
uploadtime,
#{id},
#{name},
#{filepath},
#{uploadtime},
 

좋은 웹페이지 즐겨찾기