使用Nativescript App上传文件时出错

时间:2019-04-23 14:51:09

标签: java android spring-boot angular2-nativescript

我正在尝试使用Nativescript将文件从Android手机上传到本地服务器。我得到一个响应代码-1,但问题是有时此上载有效,因此很难知道原因。发生这种情况时,服务器或电话上都没有例外。

// file path and url
   let params = [{ name: "test", value: "value" }];

   for (let i in this.filesToUpload) {
        let file = this.filesToUpload[i];
        let param = { 
            name: file.substr(file.lastIndexOf("/") + 1), 
            value: file, 
            filename: file, 
            mimeType: "image/jpeg" 
        };
        params.push(param);
    }



    // upload configuration
    var session = httpbackground.session("file-upload");
    var request = {
            url: "http://192.168.0.3:8080/accounting/payments/create",
            method: "POST",
            headers: {
                "Content-Type": "application/octet-stream",
                "name": 'name'
            },
            description: "Upload payments",
            androidAutoDeleteAfterUpload: true
        };

    var task = session.multipartUpload(params, request);

我试图在AndroidManifest.xml上添加android:usesCleartextTraffic="true",但我认为这可能不是问题,因为以下调用有效。

getString("http://192.168.0.3:8080/accounting/payments/get")
    .then((r: string) => {
        console.log(r);
    }, (e) => {
        console.log(e);
    });

const httpModule = require("http");
httpModule.request({
    url: "http://192.168.0.3:8080/accounting/payments/post",
    method: "POST",
    headers: { "Content-Type": "application/json" },
    content: JSON.stringify({
        username: "Tebogo",
        password: "passpas1"
    })
}).then((response) => {
    console.log(response);
    const result = response.content.toJSON();
}, (e) => {
    console.log(e);
});

这些是我的Spring Boot Controller上的方法,可能是我做错了什么。

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/payments")
public class PaymentsController {

    @RequestMapping(value = "create", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE,
            MediaType.APPLICATION_OCTET_STREAM_VALUE })
    public void handleFileUpload(HttpServletRequest servletRequest, @RequestParam MultipartFile[] images) {

        Map<String, String[]> parameterMap = servletRequest.getParameterMap();
        for (Entry<String, String[]> as : parameterMap.entrySet()) {
            System.out.println(as.getValue());
        }

        for (MultipartFile as : images) {
            System.out.println(as.getName());
        }

    }

    @RequestMapping(value = "post", method = RequestMethod.POST)
    public Map<String, Object> post(@RequestBody Object param) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("date", new Date());
        map.put("param", param);
        return map; 
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public String get() {
        return "gotten";
    }

}

注意::该文件上传有时有效。

0 个答案:

没有答案