0

我正在尝试将多部分表单数据从 Web 客户端上传到 Spring MVC 控制器。我在“Spring”实现中搜索以下 curl 命令:

curl -v -X POST -F "logo=@walmart.jpg" -F "id=12345" -F "name=Walmart" http://localhost:8080/api/cardprovider/logo

请注意:我不是通过 html 表单上传表单,这不是我想要实现的。

现在我的问题是:

  • 您会按照本 博客中的说明使用 Apache Commons HttpClient吗?
  • 或者是否有一种“春天”的魔法图书馆可以帮助我做到这一点?

我花了几天时间寻找使用 Spring 库的解决方案,但 Spring 手册中的所有示例都指的是 html 表单上传,或者非常基本和简单,只有基本文本。

Spring 对我来说相当新,但是里面有这么多很棒的库,如果 Spring 的人不会想到让上传多部分数据变得更容易的东西,我会感到惊讶(看起来好像它会是从表格发送)。

上面的 curl 命令与以下服务器端代码一起成功运行:

控制器:

@Controller
@RequestMapping("/api/cardprovider/logo")
public class CardproviderLogoResourceController {

  @Resource(name = "cardproviderLogoService")
  private CardproviderLogoService cardproviderLogoService;

  /**
   * Used to upload a binary logo of a Cardprovider through a Multipart request. The  id is provided as form element.
   * <p/>
   * Example to upload a file using curl:
   * curl -v -X POST -F "image=@star.jpg" -F "id=12345" -F "name=Walmart" http://localhost:8080/api/cardprovider/logo
   * <p/>
   *
   * @param logo the Multipart request
   * @param id    the Cardprovider id
   * @param name  the Cardprovider name
   */
  @RequestMapping(value = "", method = RequestMethod.POST)
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void storeCardproviderLogo(@RequestParam(value = "logo", required = false) MultipartFile logo,
                                  @RequestParam(value = "name") String name,
                                  @RequestParam(value = "id") String id) throws IOException {

    cardproviderLogoService.storeLogo(logo, id, name);
  }
}

这是我的服务类,它将 Multipart 请求存储到 GridFS 数据库中:

服务:

@Service
public class CardproviderLogoService {

  @Autowired
  GridFsOperations gridOperation;

  /**
   * Create the logo in MongoDB GridFS with the data returned from the Multipart
   * <p/>
   *
   * @param logo the MultipartFile content
   * @param id      the Cardprovider id
   * @param name    the Cardprovider name
   * @return true if the image can be saved in the database, else false
   */
  public Boolean storeLogo(MultipartFile logo, String id, String name) {
    Boolean save_state = false;

    BasicDBObject metadata = new BasicDBObject();
    metadata.put("cardproviderId", id);
    metadata.put("cardproviderName", name);
    metadata.put("contentType", logo.getContentType());
    metadata.put("fileName", createLogoFilename(name, logo.getOriginalFilename()));
    metadata.put("originalFilename", logo.getOriginalFilename());
    metadata.put("dirShortcut", "cardproviderLogo");
    metadata.put("filePath", "/resources/images/cardproviders/logos/");

    try {
        gridOperation.store(logo.getInputStream(),
                metadata.getString("fileName").toLowerCase().replace(" ", "-"),
                metadata);
        save_state = true;
    } catch (Exception ex) {
        Logger.getLogger(CardproviderLogoService.class.getName())
                .log(Level.SEVERE, "Storage of Logo failed!", ex);
    }

    return save_state;
}

/**
 * Creates the new filename before storing the file in MongoDB GridFS. The filename is created by taking the
 * name of the Cardprovider, lowercase the name and replace the whitespaces with dashes. At last the file
 * extension is added that was provided by the original filename.
 * <p/>
 *
 * @param name the Cardprovider name
 * @param originalFilename the original filename
 * @return the new filename as String
 */
private String createLogoFilename(String name, String originalFilename) {
    String cpName = name.toLowerCase().replace(" ", "-");

    String extension = "";
    int i = originalFilename.lastIndexOf('.');
    if (i > 0 && i < originalFilename.length() - 1) {
        extension = originalFilename.substring(i + 1).toLowerCase();
    }
    return cpName + "." + extension;
  }
}

感谢您的帮助和亲切的问候,克里斯

解决方案

我可以通过以下方式解决问题。我有我的 HtmlController,它采用 Input 形式并建立与 RestController 的连接以传输文件。

下面是 HtmlController 的方法:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addCardprovider(
        @ModelAttribute("cardproviderAttribute") Cardprovider cardprovider,
        Model model) {

    // Prepare acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.APPLICATION_XML);

    // Prepare header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);
    // Pass the new person and header
    HttpEntity<Cardprovider> entity = new HttpEntity<Cardprovider>(
            cardprovider, headers);

    // Send the request as POST
    try {
        // Save Cardprovider details
        ResponseEntity<Cardprovider> response = restTemplate.exchange(
                "http://localhost:8080/api/cardprovider", HttpMethod.POST,
                entity, Cardprovider.class);

        // Save Cardprovider logo
        String tempDir = System.getProperty("java.io.tmpdir");
        File file = new File(tempDir + "/" + cardprovider.getLogo().getOriginalFilename());
        cardprovider.getLogo().transferTo(file);

        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
        parts.add("id", response.getBody().getId());
        parts.add("name", response.getBody().getName());
        parts.add("logo", new FileSystemResource(file));

        restTemplate.postForLocation("http://localhost:8080/api/cardprovider/logo", parts);

    } catch (Exception e) {
        e.printStackTrace();
    }

    // This will redirect to /web/cardprovider/getall
    return "redirect:/web/cardprovider/getall";
}
4

1 回答 1

0

我对春天一无所知。在我的圈子中,使用 Apache Commons HttpClient 执行此类任务是常见的做法。

更新

当然 Spring 对此提供了支持 - 抱歉,我忽略了最明显的:RestTemplate

XML:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
 <property name="messageConverters">
  <list>
   <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
   <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
  </list>
 </property>
</bean>

爪哇:

MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("key", value);
...
form.add("file", new FileSystemResource(file));
restTemplate.postForLocation(url, form);
于 2013-02-07T23:12:02.470 回答