Java实现文件传输

方法一:

controller层

@RequestMapping(value ="/addSpot",

        method = RequestMethod.POST,

        produces = {"application/json;charset=UTF-8"})

@ResponseBody

public ChinaMapBackResult addSpot(@RequestParam("spotName")String spotName, @RequestParam("file")MultipartFile file){

        ChinaMapBackResult result;

        Spot spot =new Spot();

        spot.setSpot_name(spotName);

        try {

                AllResult allResult =spotService.saveSpot(spot,file);

                result =new ChinaMapBackResult(true,allResult);

        }catch (Exception e){

        logger.error(e.getMessage(),e);

        result =new ChinaMapBackResult(false,"服务器内部错误");

        }

        return result;

}

service层

public AllResult saveSpot(Spot spot, MultipartFile file)throws Exception{

    try{

        Spot spot1 =spotDao.getSpotByName(spot.getSpot_name(),spot.getSpot_area_id());

        if(spot1 ==null){

                if(file !=null){

                        // 生成图片存储的名称,UUID 避免相同图片名冲突

                        String originalFileName = file.getOriginalFilename();// 原始文件名

                        String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));// 图片后缀

                        String fileName = UUID.randomUUID().toString() + suffix;

                        String filePath = Constants.IMG_PATH_SPOT + fileName;

                        String sqlPath ="uploadimg/spotImg/"+fileName;

                        File saveFile =new File(filePath);

                        try {

                                // 将上传的文件保存到服务器文件系统

                                    file.transferTo(saveFile);

                                // 记录服务器文件系统图片名称

                                    spot.setSpot_img(sqlPath);

                        }catch (IOException e) {

                                e.printStackTrace();

                        }

                        }

                        String spotName = spot.getSpot_name();

                        spotDao.addSpot(spotName,spotImg);

                        return new AllResult(StateEnum.ADD_SUCCESS);

                }

                return new AllResult(StateEnum.FAILURE);

        }catch (Exception e) {

                logger.error(e.getMessage(),e);

                throw e;

        }

}

方法二:

@Controller

public class uploadImage {

        @RequestMapping(value ="upload", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})

        @ResponseBody

        public String uploadImage(HttpServletRequest request, @RequestParam("description") String description, @RequestParam("file")MultipartFile file)  {

        try {

                System.out.println(description);

                if (!file.isEmpty()) {

                // 上传文件路径

                String path = request.getServletContext().getRealPath("/imagesTest/");

                // 上传文件

                String filename = file.getOriginalFilename();

                File filepath =new File(path, filename);

                if (!filepath.getParentFile().exists()) {

                        filepath.getParentFile().mkdirs();

                }

                // 将上传的文件保存在一个目标文件中

                file.transferTo(new File(path + File.separator + filename));

                return "success";

                }else {

                        return "error";

                }

                }catch (Exception e) {

                        return "exception";

                }

        }

}

方法三:

你可能感兴趣的:(Java实现文件传输)