Crashed. who helped to change a java and c # Program to php? thank you-php Tutorial.

Source: Internet
Author: User
Crashed. who helped to change a java and c # Program to php? thank you.
HttpClient oHttpClient = new HttpClient (); // make post-method PostMethod post = new PostMethod (_ sPostUri); post. setRequestHeader ("FileExt", sFileExt); // post. setRequestHeader ("Content-Type", "multipart/form-data"); if (_ btContent! = Null) {ByteArrayRequestEntity entity = new ByteArrayRequestEntity (_ btContents); post. setContentChunked (true); post. setRequestEntity (entity);} int httpResult = 0; try {httpResult = oHttpClient.exe cuteMethod (post); // Get the name of the returned file sfilename = post. getResponseBodyAsString ();} catch (Exception ex) {post. releaseConnection (); System. out. println ("failed to submit data");} finally {post. releaseConnection ();}


temp_WebClient = new WebClient();temp_WebClient.Headers.Add("FileExt", "jpg");fileBytes = File.ReadAllBytes("c:\\1.jpg");responseArray = temp_WebClient.UploadData("http://localhost/fileuploader.do", "POST", fileBytes);resStr = Encoding.UTF8.GetString(responseArray);


Both programs post a file to a website. how can I use php to implement the file? please check the code. thank you!


Reply to discussion (solution)

To post a file to a website, you can use curl to submit the file in php. You can search for usage.

I tried it and reported a bunch of errors. I don't know anything about java. I didn't have any problems when I submitted the test to my php server, after the do file can be formally submitted to another java Server, an error is reported. it takes several days to get depressed and ask for help.

$uploadFile = file_get_contents($filePath);//content boundary $boundary   = md5(time());$postStr  = "";$postStr .="--".$boundary."\r\n";$postStr .="Content-Disposition: form-data; name=\"\"; filename=\"a.txt\"\r\n";$postStr .="Content-Type: text/plain\r\n\r\n";$postStr .=$uploadFile."\r\n";$postStr .="--".$boundary."\r\n";                                                                                                                                            /** use curl instead **/$cl = curl_init($url);$boundary = md5(time());curl_setopt($cl,CURLOPT_POST,true);curl_setopt($cl,CURLOPT_HTTPHEADER,array(        "Content-Type: multipart/form-data; boundary=".$boundary));curl_setopt($cl,CURLOPT_POSTFIELDS,$postStr);curl_setopt($cl,CURLOPT_RETURNTRANSFER,true);$content = curl_exec($cl);curl_close($cl);echo $content;


java.lang.NullPointerExceptionjava.lang.NullPointerExceptionat com.trs.infra.support.file.FilesMan.getNextFileName(FilesMan.java:562)at com.abs.infra.support.file.FilesMan.getNextFilePathName(FilesMan.java:679)at com.abs.webframework.controler.servlet.FileUploader.service(FileUploader.java:77)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)at com.abs.webframework.controler.servlet.NoLoginServiceControler.service(NoLoginServiceControler.java:110)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)at com.abs.servlet.LoginCheckFilter.doFilter(LoginCheckFilter.java:107)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)at com.abs.cluster.ext.wcm.ClusterProxyFilter.doFilter(ClusterProxyFilter.java:65)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)at com.abs.infra.session.util.SessionFilterBase.doFilter(SessionFilterBase.java:73)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)at java.lang.Thread.run(Thread.java:619)

$ File = realpath ('gif/1.gif '); // the file to be uploaded $ fields ['F'] = '@'. $ file; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields); curl_exec ($ ch );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.