Heim >Backend-Entwicklung >PHP-Tutorial >崩溃了,谁帮忙把一个java和c#的程序改成php的,谢谢
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.executeMethod(post); //获取返回值 文件名称 sfilename = post.getResponseBodyAsString(); } catch (Exception ex) { post.releaseConnection(); System.out.println("提交数据时失败"); } 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);
如果要实现向一个网址post一个文件,php中可以用curl提交。你可以搜索一下用法。
试过了,报了一堆错,我对java是一窍不通,我测试提交到我的php服务器端一点问题都没有,可正式提交到另外一台java服务器的do文件后,就报错,几天了,郁闷死,求助。
$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.NullPointerException at 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'); //要上传的文件$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);