我已经包含了所有jar文件。这个小程序实际上只在localhost中调用一个restful web服务。当我刚刚运行JarExampleApplet.java作为" run-as applet"时,它运行正常。但是当我在浏览器中运行它时它是给我一个运行时错误" java.lang.NoClassDefFoundError:com.sun.jersey.api.client.config.ClientConfig"
import java.applet.Applet;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.core.util.Base64;
@SuppressWarnings("serial")
public class JarExampleApplet extends Applet {
public static final String BASE_URI = "http://localhost:8080/RESTfulWS";
public static final String PATH_NAME = "/file/upload_json/";
public void init ()
{
JarExampleApplet my_client = new JarExampleApplet();
File file_upload = new File("C:/Grails.pdf");
try{
my_client.sendFileJSON(file_upload);
}
catch(Exception ex)
{
System.out.println("Exception : " + ex);
}
}
public void paint(Graphics g) {
g.drawString("This Applet was read from a .jar file.", 0, 25);
}
public void sendFileJSON(File file_upload) throws JSONException, IOException{
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource resource = client.resource(BASE_URI);
WebResource service = resource.path("rest").path(PATH_NAME);
JSONObject data_file = new JSONObject();
data_file.put("file_name", file_upload.getName());
data_file.put("file", convertFileToString(file_upload));
ClientResponse client_response = service.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, data_file);
System.out.println(" "+client_response);
System.out.println("Status: "+client_response.getStatus());
client.destroy();
}
//Convert my file to a Base64 String
private String convertFileToString(File file) throws IOException{
byte[] bytes = Files.readAllBytes(file.toPath());
return new String(Base64.encode(bytes));
}
}
//////////////////////// HTML文件
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<applet code="JarExampleApplet.class" archive="JarExampleApplet.jar" height="200" width="400"></applet>
</body>
</html>