小编典典

Phonegap FileUpload Java服务器

java

我正在尝试在Java Server上上传图像。文件正在从android设备传输,但在server上保存了null。

这是服务器代码

public UploadMediaServerResponse uploadFileForFunBoard(@FormDataParam("photoPath") InputStream photoInputStream,
            @FormDataParam("photoPath") FormDataContentDisposition photoFileDetail,
            @FormDataParam("userId") int userId, @FormDataParam("mediaType") String mediaType,
            @FormDataParam("title") String title,@FormDataParam("funBoardId") int funBoardId)
    {

        MediaContenModel mediaContenModel = new MediaContenModel();
        mediaContenModel.setFunBoardId(funBoardId);
        mediaContenModel.setMediaType(mediaType);
        mediaContenModel.setUserId(userId);

        UploadMediaServerResponse uploadMediaServerResponse = new UploadMediaServerResponse();
        boolean isMediaProcessedAndUploaded = true;
        String mediaProcessingError = "";

        if (photoInputStream != null && photoFileDetail != null)
        {
            uploadMediaServerResponse = mediaService.uploadOnServer(photoInputStream,
                    photoFileDetail.getFileName(), userId+"");
            if (uploadMediaServerResponse != null
                    && !uploadMediaServerResponse.getMediaUrl().equalsIgnoreCase("ERROR"))
            {
                mediaContenModel.setImageUrl(uploadMediaServerResponse.getMediaUrl());
                logger.debug("ContentService --> createStroyline --> fearture Image url ::"
                        + uploadMediaServerResponse.getMediaUrl());
            }
            else
            {
                isMediaProcessedAndUploaded = false;
                mediaProcessingError = uploadMediaServerResponse.getMediaUrl();
                logger.debug("ContentService --> createStroyline -->  mediaProcessingError ::"
                        + mediaProcessingError);
            }
        }

        if (isMediaProcessedAndUploaded)
        {
            UploadMediaServerResponse response = funBoardService.uploadMediaContent(mediaContenModel);
            uploadMediaServerResponse.setMediaUrl(response.getMediaUrl());
        }
        else
        {
            uploadMediaServerResponse.setError("Task Failed");
            uploadMediaServerResponse.setStatus(ServiceAPIStatus.FAILED.getStatus());
        }
        return uploadMediaServerResponse;
    }

这是我的电话间隙代码

var pictureSource;   
var destinationType;


function onPhotoURISuccess(imageURI) 
{
    console.log(imageURI);
    var largeImage = document.getElementById('largeImage');
    largeImage.style.display = 'block';
    largeImage.src = imageURI;










 var options = new FileUploadOptions();
            options.fileKey="photoPath";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";



                options.params = {
                            "userId": 1,
                            "funBoardId": 3,
                            "mediaType": 'image'
                        };


            console.log(JSON.stringify(options));

            var ft = new FileTransfer();
            ft.upload(imageURI, _BaseURL+"mobile/userService/funboard/upload", win, fail, options);



}

function onPhotoDataSuccess(imageURI) 
{ 
    var imgProfile = document.getElementById('imgProfile');
    imgProfile.src = imageURI;
    if(sessionStorage.isprofileimage==1)
    {
        getLocation();
    }
    movePic(imageURI);
}

function onFail(message) 
{
    alert('Failed because: ' + message);
}

function movePic(file)
{ 
    window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
}

function resolveOnSuccess(entry)
{ 
    var d = new Date();
    var n = d.getTime();
    var newFileName = n + ".jpg";
    var myFolderApp = "MyAppFolder";
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) 
    {      
        fileSys.root.getDirectory( myFolderApp,
                {create:true, exclusive: false},
                function(directory) 
                {
                    entry.moveTo(directory, newFileName,  successMove, resOnError);
                },
        resOnError);
    },
    resOnError);
}

function successMove(entry) 
{
    alert(entry.fullPath);
    sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) 
{
    alert(error.code);
}

function capturePhotoEdit() 
{
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
    destinationType: destinationType.DATA_URL });
}

function getPhoto(source) 
{
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}

function onFail(message) 
{
    alert('Failed because: ' + message);
 }



function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
            alert(r.response);
        }

        function fail(error) {
            alert("An error has occurred: Code = " = error.code);
        }

04-14 19:33:46.0​​10:E /
FileTransfer(13550):java.io.FileNotFoundException:http:/// jeeyoh / mobile /
userService / funboard / upload

提前致谢


阅读 204

收藏
2020-11-30

共1个答案

小编典典

更换

     options.fileKey="file";

     options.fileKey="photoPath";
2020-11-30