程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Spring MVC中使用不带表单的Multipart大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在Spring MVC中使用不带表单的Multipart?

开发过程中遇到在Spring MVC中使用不带表单的Multipart的问题如何解决?下面主要结合日常开发的经验,给出你关于在Spring MVC中使用不带表单的Multipart的解决方法建议,希望对你解决在Spring MVC中使用不带表单的Multipart有所启发或帮助;

我的方法存在问题:

我为MultiPartResolver创建了一个bean。解决问题后的理解是,仅当你需要特定类型的文件或非常特定于应用程序的文件时,才定义此bean。尽管我希望对此有更多了解,并希望能从stackoverflow的技术人员那里听到。

当前问题的解决方案:

我会给出我的源代码,

HTML:

<div ng-controller="myCtrl">
        <input type="file" file-model="myfile" />
        <button ng-click="uploadfile()">upload me</button>
    </div>

AngularJs:

     var myApp = angular.module('myApp', []);

        myApp.directive('fileModel', ['$parse', function ($parsE) {
            return {
                re@R_197_10495@ct: 'A',
                link: function(scope, element, attrs) {
                    var model = $parse(attrs.fileModel);
                    var modelSetter = model.assign;

                    element.bind('change', function(){
                        scope.$apply(function(){
                            modelSetter(scope, element[0].files[0]);
                        });
                    });
                }
            };
        }]);
        myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){

            $scope.uploadfile = function(){
                var file = $scope.myfile;
                var fd = new FormData();
                fd.append('file', filE);
    //We can send anything in name parameter, 
//it is hard coded to abc as it is irrelavant in this case.
                var uploadUrl = "/upload?name=abc";
                $http.post(uploadUrl, fd, {
                    transformrequest: angular.IDentity,
                    headers: {'Content-Type': undefineD}
                })
                .success(function(){
                })
                .error(function(){
                });
            }

        }]);

spring:

@requestMapPing(value="/upload", method=requestMethod.POST)
    public @R_197_10495@ng handlefileUpload(@requestParam("name") @R_197_10495@ng name,
            @requestParam("file") multipartfile filE){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                bufferedoutputstream stream =
                        new bufferedoutputstream(new fiLeoutputStream(new file(Name)));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + "!";
            } catch (Exception E) {
                return "You Failed to upload " + name + " => " + e.getmessage();
            }
        } else {
            return "You Failed to upload " + name + " because the file was empty.";
        }
    }

@arahant即使发送请求时在请求有效负载中没有看到任何文档base64内容,Angular也会发送multipartfile。

解决方法

Controller @Spring :

大佬总结

以上是大佬教程为你收集整理的在Spring MVC中使用不带表单的Multipart全部内容,希望文章能够帮你解决在Spring MVC中使用不带表单的Multipart所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。