java.lang.NoClassDefFoundError代码部分来自网络,这里引用的是apache给的开源jar包,实现很方便的,(commons-httpclient-3.1android客户端使用),(commons-fileupload-1.2.2,commons-io-2.4,servlet的使用,记得把后面两个jar包放在 C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext目录下)
下面贴贴代码吧:
httpclict如下:
package com.example.http ;import java.io.File ;import org.apache.commons.httpclient.HttpClient ;import org.apache.commons.httpclient.HttpStatus ;import org.apache.commons.httpclient.methods.PostMethod ;import org.apache.commons.httpclient.methods.multipart.FilePart ;import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity ;import org.apache.commons.httpclient.methods.multipart.Part ;public class Hclient { /* * private Context mContext ; * * public Hclient ( Context c ) { this.mContext = c ; } */ public void UpLoadFile ( String str) { String targetURL = null ;// TODO 指定URL File targetFile = null ;// TODO 指定上传文件 targetFile = new File ( str) ; targetURL = "http://192.168.1.100:8081/http/Http" ; // servleturl PostMethod filePost = new PostMethod ( targetURL ) ; try { // 通过以下方法可以模拟页面参数提交 // filePost.setParameter("name", "中文"); // filePost.setParameter("pass", "1234"); Part [ ] parts = { new FilePart ( targetFile.getName ( ) , targetFile ) } ; filePost.setRequestEntity ( new MultipartRequestEntity ( parts , filePost.getParams ( ) ) ) ; HttpClient client = new HttpClient ( ) ; client.getHttpConnectionManager ( ).getParams ( ) .setConnectionTimeout ( 5000 ) ; int status = client.executeMethod ( filePost ) ; if ( status == HttpStatus.SC_OK ) { System.out.println ( "上传成功" ) ; // 上传成功 } else { System.out.println ( "上传失败" ) ; // 上传失败 } } catch ( Exception ex ) { ex.printStackTrace ( ) ; } finally { filePost.releaseConnection ( ) ; } } }
Activity如下:
package com.example.http ;import android.app.Activity ;import android.os.Bundle ;import android.os.StrictMode ;import android.view.View ;import android.widget.Button ;public class HttpMainActivity extends Activity { private Button mButton ; private String str1 = "/sdcard/http.txt" ; private String str2 = "/sdcard/http.mp3" ; private int mFlag = 0 ; Hclient hclient ; @ Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate ( savedInstanceState ) ; StrictMode .setThreadPolicy ( new StrictMode.ThreadPolicy.Builder ( ) .detectDiskReads ( ).detectDiskWrites ( ) .detectNetwork ( ).penaltyLog ( ).build ( ) ) ; StrictMode.setVmPolicy ( new StrictMode.VmPolicy.Builder ( ) .detectLeakedSqlLiteObjects ( ) .detectLeakedClosableObjects ( ).penaltyLog ( ) .penaltyDeath ( ).build ( ) ) ; setContentView ( R.layout.activity_http_main ) ; mButton = ( Button ) findViewById ( R.id.button ) ; hclient = new Hclient ( ) ; mButton.setOnClickListener ( new View.OnClickListener ( ) { @ Override public void onClick( View v ) { mFlag ++ ; if ( mFlag == 1 ) { hclient.UpLoadFile ( str1 ) ; } else if ( mFlag == 2 ) { hclient.UpLoadFile ( str2 ) ; } else { } } } ) ; } }
记得加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.INTERNET"/>
t添加jar包的时候可能编译不过,java.lang.NoClassDefFoundError错误,下面给出办法:
1,添加jar包
2,把jar包放在libs文件中,如图
下面是servlet代码:
import java.io.File ;import java.io.IOException ;import java.util.Iterator ;import java.util.List ;import javax.servlet.ServletException ;import javax.servlet.http.HttpServlet ;import javax.servlet.http.HttpServletRequest ;import javax.servlet.http.HttpServletResponse ;import org.apache.commons.fileupload.FileItem ;import org.apache.commons.fileupload.disk.DiskFileItemFactory ;import org.apache.commons.fileupload.servlet.ServletFileUpload ;public class TestServlets extends HttpServlet { private String uploadPath = "D:\\temp" ; // 上传文件的目录 private String tempPath = "d:\\temp\\buffer\\" ; // 临时文件目录 private File tempPathFile ; public void init( ) throws ServletException { File uploadFile = new File ( uploadPath ) ; if ( ! uploadFile.exists ( ) ) { uploadFile.mkdirs ( ) ; } File tempPathFile = new File ( tempPath ) ; if ( ! tempPathFile.exists ( ) ) { tempPathFile.mkdirs ( ) ; } } public void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory ( ) ; // Set factory constraints factory.setSizeThreshold ( 4096 ) ; // 设置缓冲区大小,这里是4kb factory.setRepository ( tempPathFile ) ;// 设置缓冲区目录 // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload ( factory ) ; // Set overall request size constraint upload.setSizeMax ( 4194304 ) ; // 设置最大文件尺寸,这里是4MB List < FileItem > items = upload .parseRequest ( request ) ;// 得到所有的文件 Iterator < FileItem > i = items.iterator ( ) ; while ( i.hasNext ( ) ) { FileItem fi = ( FileItem ) i.next ( ) ; String fileName = fi.getName ( ) ; if ( fileName != null ) { File fullFile = new File ( fi.getName ( ) ) ; File savedFile = new File ( uploadPath , fullFile.getName ( ) ) ; fi.write ( savedFile ) ; } } System.out.print ( "upload succeed" ) ; } catch ( Exception e ) { System.out.println ( e.getMessage ( ) ) ; // 可以跳转出错页面 e.printStackTrace ( ) ; } } }
点击打开链接,点击这个是jar包的下载地址,我已经上传csdn了
- 1楼cdqn20073天前 10:51
- 多谢博主分享!!