我正在尝试通过我的 java 代码执行文件附件操作。但它在我的控制台输出中返回“java.lang.NoClassDefFoundError”。你能帮我解决这个问题吗?休息我的代码没有任何问题。
以下是我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
public static void main(String[] args) throws Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://watsonexp-stg.corp.adobe.com/watson/api/bug/addAttachmentAPI.jsp");
//to make a multipart POST request
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
multipartEntity.addPart("appGUID", new StringBody("e2513b90-f327-4cc0-8c07-2d79a1b6eddd"));
multipartEntity.addPart("userId", new StringBody("karansha"));
multipartEntity.addPart("password", new StringBody("RGVsbCUxMDA="));
multipartEntity.addPart("bugId", new StringBody("3402114"));
multipartEntity.addPart("fileDescription", new StringBody("trying to attach a file"));
multipartEntity.addPart("external", new StringBody("false"));
multipartEntity.addPart("Filedata", new FileBody(new File("C:/Users/karansha/Desktop/test.jpg")));
httpPost.setEntity(multipartEntity);
HttpResponse res= httpClient.execute(httpPost);
//following lines will print out the respose from the server on attempting to upload a file.
HttpEntity httpEntity = res.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
}
}