我绝对不是电脑大师。 :)
尝试通过applet在我的网站上传文件时出现http 403错误。 这意味着它是被禁止的。也许这是一种常规行为,可能不允许通过http协议上传。这是真的吗?那怎么办呢?我希望我的applet能够在服务器的特定文件夹上上传小文件。
这是代码:
private static String folder = "http://..." //URL of the folder to upload to
public void saveScore(Item hs) { //Item is a serializable object to save
String filename = "s"+Integer.toString(hs.getScore())+".sco" ; // name of the file
System.out.println("*** Trying to save file to : " + filename) ;
try {
//setting connection
HttpURLConnection con = (HttpURLConnection) new URL(folder+"/"+filename).openConnection() ;
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty ("Content-Type", "multipart/form-data");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
con.setChunkedStreamingMode(1024);
con.setRequestMethod("PUT") ;
ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream()) ;
//uploading
oos.writeObject(hs) ;
oos.flush() ;
oos.close();
//getting answer
DataInputStream is = new DataInputStream(con.getInputStream());
String s = is.readLine();
is.close();
System.out.println("** Answer **");
System.out.println(s) ;
} catch (IOException e) {
e.printStackTrace(System.out) ; // gives me a 403 error
}
}
感谢您的帮助...
答案 0 :(得分:0)
浏览链接。希望它会对你有所帮助。
http://stackoverflow.com/questions/1599018/java-applet-to-upload-a-file?rq=1