Help about save text files to web server
in
Integration and Hardware
•
1 year ago
Hi, everyone.
I am having a hard time to update a txt file (permission set to 777) on web server by using the code here:
http://wiki.processing.org/index.php?title=Saving_files_to_a_web-server
I have a txt file name data.txt and a php file named update.php at http://www.mydomain.com/Online/outgoing/, and all the codes are shown below. But when I ran the program, I got an error, and do not know how to fix it. Can anyone help me? Any hint and guide is very appreciated. Thanks
Bo
The code of processing on client part is :
The php file on server side is shown below:
But after running the code, I got this error:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.mydomain.com/Online/outgoing/saveFile.php?title=data_20120928-233150&ext=txt&folder=http://www.mydomain.com/Online/outgoing/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at sketch_120928b.postData(sketch_120928b.java:94)
at sketch_120928b.saveToWeb_saveFileString(sketch_120928b.java:52)
at sketch_120928b.setup(sketch_120928b.java:45)
at processing.core.PApplet.handleDraw(PApplet.java:2103)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)
I am having a hard time to update a txt file (permission set to 777) on web server by using the code here:
http://wiki.processing.org/index.php?title=Saving_files_to_a_web-server
I have a txt file name data.txt and a php file named update.php at http://www.mydomain.com/Online/outgoing/, and all the codes are shown below. But when I ran the program, I got an error, and do not know how to fix it. Can anyone help me? Any hint and guide is very appreciated. Thanks
Bo
The code of processing on client part is :
- String men;
String[] data;
String url;
String title;
String ext;
String folder;
boolean popup;
void setup() {
title="data";
ext="txt";
url = "http://www.mydomain.com/Online/outgoing/";
folder = "http://www.mydomain.com/Online/outgoing/";
men = "Chernenko,Andropov, Brezhnev";
data = split(men, ',');
popup = false;
saveToWeb_saveFileString(title, ext, folder, data, popup);
}
void draw(){ - //do nothing
- }
void saveToWeb_saveFileString(String title, String ext, String folder, String[] data, boolean popup)
{
println("SAVING File START");
postData(title+"_"+year()+nf(month(),2)+nf(day(),2)+"-"+nf(hour(),2)+nf(minute(),2)+nf(second(),2),ext,folder,join(data,"\n").getBytes(),popup);
println("SAVING File STOP");
}
void postData(String title, String ext, String folder, byte[] bytes, boolean popup)
{
try{
URL u = new URL(url+"saveFile.php?title="+title+"&ext="+ext+"&folder="+folder);
URLConnection c = u.openConnection();
// post multipart data
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set request headers
c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A");
// open a stream which can write to the url
DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
// write content to the server, begin with the tag that says a content element is comming
dstream.writeBytes("--AXi93A\r\n");
// discribe the content
dstream.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"whatever\" \r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n");
dstream.write(bytes,0,bytes.length);
// close the multipart form request
dstream.writeBytes("\r\n--AXi93A--\r\n\r\n");
dstream.flush();
dstream.close();
// read the output from the URL
try{
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String sIn = in.readLine();
boolean b = true;
while(sIn!=null){
if(sIn!=null){
if(popup) if(sIn.substring(0,folder.length()).equals(folder)) link(url+sIn, "_blank");
System.out.println(sIn);
}
sIn = in.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
e.printStackTrace();
}
}
The php file on server side is shown below:
- ;<?
$Title = $_GET['title'];
$Ext = $_GET['ext'];
$folder = $_GET['folder'];
$savepath = dirname($_SERVER["PATH_TRANSLATED"]);
$filename = $Title.".".$Ext;
while(file_exists($folder."/".$filename))
$filename = $Title."-".rand(2,500).".".$Ext;
if (is_uploaded_file($data))
{
$newfile = $savepath."/".$folder."/".$filename;
if (!copy($data, $newfile))
{
// if an error occurs the file could not
// be written, read or possibly does not exist
echo "Error #1 Uploading File.";
exit();
}else{
echo $folder."/".$filename;
}
} else {
echo "Error #2 Uploading File.";
exit();
}
?>
But after running the code, I got this error:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.mydomain.com/Online/outgoing/saveFile.php?title=data_20120928-233150&ext=txt&folder=http://www.mydomain.com/Online/outgoing/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at sketch_120928b.postData(sketch_120928b.java:94)
at sketch_120928b.saveToWeb_saveFileString(sketch_120928b.java:52)
at sketch_120928b.setup(sketch_120928b.java:45)
at processing.core.PApplet.handleDraw(PApplet.java:2103)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)
1