2016-10-08 10 views
0

私はJobOfferを収集し、手動でそれらに適用することができます私の友達のアプリで書いています。POSTを介してInputStreamからアップロードファイル中のSpringブート、RestTemplate例外

たとえば、私はこのページからCSRFトークンを取得していますClick原因私はファイルをアップロードするためにCSRFが必要です。

しかし、とき、私はエラーになっています)restTemplate.exchangeを(実行しようとしている: 「によって引き起こさ:java.lang.ClassCastExceptionがを:org.springframework.core.io.ByteArrayResourceはjava.langのにキャストすることはできません.String "ここで

は私のクラスである:ここでは

@Data 
@Service 
class StackOverflowComAcceptor implements JobOfferService { 

    private final static String BASE_URL = "http://stackoverflow.com"; 
    private final ConnectionService2 connectionService2; 
    private final StackOverflowComFactory stackOverflowComFactory; 
    private final RestTemplate restTemplate; 

    @Autowired 
    private CVProvider cvProvider; 



    @Override 
    @SneakyThrows 
    public void accept(JobOffer jobOffer) { 
     //List<Connection.KeyVal> templateDataEntries = stackOverflowComFactory.create(); 

     ConnectionRequest connectionRequest = ConnectionRequest 
       .builder() 
       //url is in joboffer 
       .url("http://stackoverflow.com/jobs/apply/110247") 
       .method(Connection.Method.GET) 
       .data(new ArrayList<Connection.KeyVal>()) 
       .build(); 


     ConnectionResponse submit1 = connectionService2.submit(connectionRequest); 
     Document document = submit1.getDocument(); 

     String csrf = extractCSRF(document); 
     String cvurl = "http://stackoverflow.com/jobs/apply/upload-resume"; 
     //String cvurl = "http://stackoverflow.com/jobs/apply/upload-resume?jobId=110247&fkey=" + csrf; 
     InputStream inputStream = cvProvider.asInputStream(); 
     byte[] bytes = IOUtils.toByteArray(inputStream); 

     Map<String, String> uriVariables = new HashMap<>(); 
     uriVariables.put("fkey", csrf); 
     uriVariables.put("jobId", "110247"); 


     HttpHeaders partHeaders = new HttpHeaders(); 
     partHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); 

     ByteArrayResource byteArrayResource = new ByteArrayResource(bytes, "test.pdf"); 

     MultiValueMap<String, Object> data = new LinkedMultiValueMap<>(); 
     data.add("qqfile", byteArrayResource); 

     HttpEntity<MultiValueMap<String, Object>> requestEntity = 
       new HttpEntity<>(data, partHeaders); 

     ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter(); 
//  byteArrayHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(new MediaType[]{MediaType 
//    .APPLICATION_OCTET_STREAM})); 
     FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); 
     formHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
       new MediaType[]{MediaType.APPLICATION_OCTET_STREAM})); 

     //restTemplate.getMessageConverters().add(byteArrayHttpMessageConverter); 
     restTemplate.getMessageConverters().add(formHttpMessageConverter); 

     ResponseEntity<Object> model = restTemplate.exchange(cvurl, HttpMethod.POST, requestEntity, Object.class , 
       uriVariables); 

     System.out.println(model); 


    } 

    private String extractCSRF(Document document) { 
     String s = document.getElementsByTag("script").get(0).toString(); 
     Pattern p = Pattern.compile("Careers.XSRF_KEY = \\\"([a-z0-9]{32})\\\""); // Regex for the value of the key 
     Matcher m = p.matcher(s); 
     String csrf = null; 
     while(m.find()) 
     { 
      csrf = m.group(1); // value only 
     } 
     System.out.println(csrf); 
     return csrf; 
    } 



} 

は私のスタックトレースです:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testStacka' defined in file [D:\WWW\Epomis\epomis\build\classes\main\net\elenx\epomis\service\com\stackoverflow\Test.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.springframework.core.io.ByteArrayResource cannot be cast to java.lang.String 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.BUILD-SNAPSHOT.jar:1.4.1.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) ~[spring-boot-1.4.1.BUILD-SNAPSHOT.jar:1.4.1.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) ~[spring-boot-1.4.1.BUILD-SNAPSHOT.jar:1.4.1.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-1.4.1.BUILD-SNAPSHOT.jar:1.4.1.BUILD-SNAPSHOT] 
    at net.elenx.Epomis.main(Epomis.java:22) [main/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74] 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na] 
Caused by: java.lang.ClassCastException: org.springframework.core.io.ByteArrayResource cannot be cast to java.lang.String 
    at org.springframework.http.converter.FormHttpMessageConverter.writeForm(FormHttpMessageConverter.java:292) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:254) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:89) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:849) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:617) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:588) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:507) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at net.elenx.epomis.service.com.stackoverflow.StackOverflowComAcceptor.accept(StackOverflowComAcceptor.java:99) ~[main/:na] 
    at net.elenx.epomis.service.com.stackoverflow.Test.afterPropertiesSet(Test.java:40) ~[main/:na] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] 
    ... 19 common frames omitted 


Process finished with exit code 1 

任意のアイデアは、この問題を解決する方法?

答えて

0

これは明らかにキャストに問題があり、FileMessageResourceをStringにキャストしようとしています。必ずしも明示的に起こっているわけではありません。スタックトレースを追加できますか?

私はあなたのために、あなたのための答えを持っているので、2人でも、あなたが一番好きなものを選んでください。

  1. あなたはこれを取ることにした場合、あなたが ) byteArrayResource.getByteArrayを(渡すために覚えて、単にHttpEntity<byte[]>を使用して検討することができるように、任意のオブジェクトを渡すことができるようにコンストラクタがある として、HttpEntity<MultiValueMap<String, Object>>を使用する必要はありません アプローチ。
  2. 私はSpring APIを見てきましたが、 FormHttpMessageConverterには、マルチパート/その他のコンテンツタイプの場合にはわずかな差異 があります。 MultiValueMap<String, Object> は、マルチパートコンテンツタイプにのみ使用できます。そうでなければ、マップは MultiValueMap<String, String>にキャストされているため、 アプリケーションの実行中にエラーが発生します。それはちょうど私がスタックトレースと実際のコードを提供してい partHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
+0

partHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);からコンテンツタイプを変更を取り除くために –

関連する問題