简体   繁体   English

如何使用angularjs和spring上传服务器文件夹中的图像

[英]how to upload image in server folder using angularjs and spring

Im using angularjs in client side and spring ,hibernate in server side. 我在客户端和spring中使用angularjs,在服务器端休眠。 Now im trying to upload the image , which could be saved in a server folder and the path where it stored can save in sql db . 现在,我正在尝试上载图像,该图像可以保存在服务器文件夹中,并且其存储路径可以保存在sql db中。 Im struggling to read the image using angularjs with spring . 我正在努力使用带有spring的angularjs读取图像。 Kindly guide me to complete my task Thanks in advance 请指导我完成任务

There is a project called Spring Content that allows you to create content management applications with very little code. 有一个名为Spring Content的项目,该项目使您可以用很少的代码来创建内容管理应用程序。 Take a look at the Getting Started guides here and here . 此处此处查看“入门指南”。

This project also has a demo application called spring docs that acts as a reference showing how an angular (1.x) front-end can talk to a Spring Content backend. 该项目还有一个名为spring docs的演示应用程序,该应用程序充当参考,展示了有角度(1.x)的前端如何与Spring Content后端进行对话。 From a file upload perspective all the work on the front-end is done by a Danial Farid's excellent ng-upload component. 从文件上传的角度来看,前端的所有工作都是由Danial Farid出色的ng-upload组件完成的。 This video then walks through the backend code showing just how easy it is to create that. 然后,该视频介绍了后端代码,显示了创建代码的难易程度。 Follow these and you should be up and running in no-time. 遵循这些步骤,您应该立即启动并运行。

Based on your additional details this would look something like this:- 根据您的其他详细信息,它看起来像这样:-

pom.xml pom.xml

<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-rest-boot-starter</artifactId>
    <version>0.0.10</version>
</dependency>
<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>content-fs-spring-boot-starter</artifactId>
    <version>0.0.10</version>
</dependency>

SpringBootApplication.java SpringBootApplication.java

@SpringBootApplication
public class YourSpringBootApplication {

  public static void main(String[] args) {
    SpringApplication.run(YourSpringBootApplication.class, args);
  }

  @Configuration
  @EnableFilesystemStores
  public static class StoreConfig {
    File filesystemRoot() {
        // return root of your image store
    }

    // this bean is the spring resource loader that will be used by
    // the product store  
    @Bean
    public FileSystemResourceLoader fsResourceLoader() throws Exception 
    {
      return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }
  }

  @StoreRestResource(path="addTest")
  public interface ProductImagesStore extends Store<String> {
    //
  }
}

This will be enough to create a backend service that your angular front-end can POST multipart file requests too and subsequently GET from. 这足以创建一个后端服务,您的前端用户也可以发布多部分文件请求,然后从中进行获取。 Note, no controller code. 注意,没有控制器代码。 Spring Content REST doesn't currently support more than one multipart file so you would have to modify test.js for call $hhtp.port multiple times, one for each product image. Spring Content REST当前不支持多个文件,因此您必须修改test.js才能多次调用$ hhtp.port,每个产品映像一个。 Also happy to work with you to add this functionality into Spring Content too. 也很高兴与您一起将此功能添加到Spring Content中。

In order upload a file along with data using angularjs and spring , here are the code ` 为了使用angularjs和spring上传文件以及数据,这里是代码`

test.js test.js

               var url = "addTest";                    
               var data = new FormData();
               data.append('vendorId', $scope.vendorId);
               data.append('categoryId', _ddlCategory);
               data.append('subCategoryId', _ddlSubCategory);
               data.append('productName', _productName);
               data.append('productImage1', 'image1');
               data.append('productImage2', 'image2');
               data.append('productImage3', 'image3');
               data.append('productImage4', 'image4');
               data.append('productImage5', 'image5');
               data.append('productImagePath1', file1);
               data.append('productImagePath2', file2);
               data.append('productImagePath3', file3);
               data.append('productImagePath4', file4);
               data.append('productImagePath5', file5);
               data.append('specification', _productspec);
               data.append('terms', _termsDescription);

               var config = {
                    transformRequest: angular.identity,
                    transformResponse: angular.identity,
                    headers : {
                        'Content-Type': undefined
                    }                      
               }    

               $http.post(url, data, config).then(function (response) {
                    $scope.uploadResult=JSON.parse(response.data);

                }, function (data, status, headers, config) {
                    //$scope.uploadResult=response.data;
                    console.log('errorresult '+ data);

                });

controller.java controller.java

@RequestMapping(value = "/addTest", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data", consumes = {
"multipart/form-data" })
public @ResponseBody <Base64Binary> String addProductTest(@RequestParam("vendorId") Long vendorId,
@RequestParam("categoryId") Long categoryId, @RequestParam("subCategoryId") Long subCategoryId,
@RequestParam("productName") String productName,
@RequestParam("productDescription") String productDescription,
@RequestParam("productQuantity") Long productQuantity, @R
@RequestParam("productImagePath1") MultipartFile file1,
@RequestParam("productImagePath2") MultipartFile file2,
@RequestParam("productImagePath3") MultipartFile file3,
@RequestParam("productImagePath4") MultipartFile file4,
@RequestParam("productImagePath5") MultipartFile file5, @RequestParam("specification") String specification,
@RequestParam("terms") String terms)
throws ApplicationException, JsonIOException, JsonSyntaxException, IOException, ParseException {

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

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