lundi 20 avril 2015

File upload in Spring

I am trying to upload an audio file using the following code on server.Right now, it works perfectly well for image files but not for audios.I think MultipartFile should work with audio files as well.Can any one tell me what is wrong here? I am getting "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." error.

Does MultipartFile not work with audio files?If no what is an alternative?

@Transactional
public BaseVO uploadImage(MultipartFile file, long userId){
    Map<String, Object> alertParams = new HashMap<String, Object>();

    try{
        if (!file.isEmpty()) {
            Image profileImage = imageRepository.findByUserId(userId);
            if(profileImage == null){
                profileImage = new Image();
                profileImage.setUserId(userId);
            }

            File dir = null;
            String realPath;
            if(liveBuild == "true"){
                realPath = liveImageUploadRepository;
                dir = new File(realPath);
            }else{
                HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
                realPath = request.getSession().getServletContext().getRealPath("/") + devImageUploadRepository;
                dir = new File(realPath);
            }
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String finalFileStorePath = realPath + "/" + userId + ".jpg";
            File path = new File(finalFileStorePath);
            file.transferTo(path);

            profileImage.setImagePath(realPath);
            //profileImage.setImageName(userId + ".jpg");
            profileImage.setImageName(userId+"");
            profileImage.setImageType(".jpg");

            imageRepository.save(profileImage);

            alertParams.put("id", profileImage.getId());
        } else {
            return new BaseVO(alertParams, Constants.STATUS_OK, Constants.STATUS_OK_MSG);
        }
    }catch(HibernateException e){
        return new BaseVO(alertParams, Constants.STATUS_ERROR, Constants.STATUS_ERROR_MSG + " " + e.getMessage());
    } catch (IllegalStateException e) {
        e.printStackTrace();
        return new BaseVO(alertParams, Constants.STATUS_ERROR, Constants.STATUS_ERROR_MSG);
    } catch (IOException e) {
        e.printStackTrace();
        return new BaseVO(alertParams, Constants.STATUS_ERROR, Constants.STATUS_ERROR_MSG);
    }
    return new BaseVO(alertParams, Constants.STATUS_OK, Constants.STATUS_OK_MSG);
}

Aucun commentaire:

Enregistrer un commentaire