简体   繁体   English

使用AngularJS和Java Spring发布表单数据

[英]Post Form data using AngularJS and Java Spring

I have a jsp file in which i am uploading a file. 我有一个jsp文件,我正在其中上传文件。 For backend handling of file i made a controller in spring. 对于文件的后端处理,我在春季制作了一个控制器。 But it return error in console: String parameter 'name' is not present ? 但是它在控制台中返回错误: 字符串参数'name'不存在 My Code is - 我的代码是-

JSP File JSP文件

<input class="fileInput" type="file" id="fileInput" accept="image/gif, image/jpeg, image/png" />
<button type="button" ng-click="saveProfile()">Update</button>

JS File JS文件

$scope.username = 01238339595;
$scope.saveProfile = function() {
                    var input = document.getElementById('fileInput');
                    if (input.files && input.files[0]) {
                        var formData = new FormData();
                        formData.append("name", $scope.username);
                        formData.append("file", input.files[0]);
                        console.log("form data " + formData);
                        $http.post("save-avatar", formData)
                            .success(function(data, status, headers,config) {
                                                        console.log('Success');
                                                    })
                            .error(function(data, status, headers, config) {
                                                        console.log('Error');
                                                    });
                    }
                };

Controller 控制者

@RequestMapping(value = "save-avatar", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file,
        HttpServletRequest request) throws IOException {
    if (file.isEmpty())
        throw new IOException("File Field is Empty");

    ServletContext servletContext = request.getSession().getServletContext();
    String absoluteDiskPath = servletContext.getRealPath("/");

    File folder = new File(absoluteDiskPath + "/avatar/");
    if (!folder.exists())
        folder.mkdirs();

    File avatarFile = new File(folder, name + ".jpg");
    if (!avatarFile.exists())
        avatarFile.createNewFile();

    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(avatarFile);
        outputStream.write(file.getBytes());
    } finally {
        if (outputStream != null)
            outputStream.close();
    }

    return "redirect:/avatar-profile?name=" + name;
}

In my config xml: 在我的配置xml中:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760" />
</bean>

Instead of the above controller method, use this: 代替上面的控制器方法,使用以下方法:

@RequestMapping(value = "/save-avatar", method = RequestMethod.POST)
public void UploadFile(MultipartHttpServletRequest request, HttpServletResponse response) {

    Iterator<String> itr = request.getFileNames();
    MultipartFile file=null;

    while (itr.hastNext()) {
        file = request.getFile(itr.next());
        String name = request.getParameter("name");

        //Do your stuff here.......
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM