package service;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import model.FileModel;
/**
*
* @author baipeng {baipeng8608@gmail.com}
*
* Apr 30, 2009
*
*/
public class FileModelService {
public static FileModel buildFileModel(int recid)throws Exception{
URL url = new URL("http://localhost:8080/FMTool_Web2/servlet/BuildFileModel?recid="+recid);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("content-type", "application/x-java-serialized-object");
conn.connect();
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
FileModel model = (FileModel)ois.readObject();
ois.close();
conn.disconnect();
return model;
}
public static List<FileModel> getFileModels() throws Exception{
URL url = new URL("http://localhost:8080/FMTool_Web2/servlet/GetFileModels");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("content-type", "application/x-java-serialized-object");
conn.connect();
List<FileModel> models = null;
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
Object iob = ois.readObject();
if(iob instanceof List){
models = (List<FileModel>)iob;
}else{
System.err.println("FileModelService#getFileModels() doesn't get RIGHT object!");
}
ois.close();
conn.disconnect();
return models;
}
/**
* �����ļ���ʱ���õ�.
* Possible Bug;
* Cann't send the outputobject to the connection.
* @param fileModel
* @throws Exception
*/
public static void updateModel(FileModel fileModel) throws Exception{
URL url = new URL("http://localhost:8080/FMTool_Web2/servlet/UpdateModel");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("content-type", "application/x-java-serialized-object");
conn.setRequestMethod("POST");
conn.connect();
ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
oos.writeObject(fileModel);
oos.flush();
oos.close();//������ܻ�������.
conn.getInputStream(); //Real connect.
conn.disconnect();
return;
}
}