做的是利用httpclient完成客户端和服务器端的交互(类似百度云客户端)
servlet:将上传的文件写成xml的数据流。
public class FileServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String opttype=request.getParameter("opttype");
String path=request.getParameter("path");
if("listfiles".equals(opttype)){
List<File> files=FileDao.getFileList(path);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<files>");
for(File file : files){
if(!file.isDirectory()){
out.println(" <file type=\"file\">");
out.println(" <path>"+file.getAbsolutePath()+"</path>");
out.println(" <name>"+file.getName()+"</name>");
out.println(" <size>"+file.getAbsoluteFile().length()+"</size>");
out.println(" <ext>"+file.getName().substring(file.getName().lastIndexOf(".")+1)+"</ext>");
out.println(" </file>");
}else{
out.println(" <file type=\"dir\">");
out.println(" <path>"+file.getAbsolutePath()+"</path>");
out.println(" <name>"+file.getName()+"</name>");
out.println(" </file>");
}
}
out.println("</files>");
}
out.close();
}
}
客户端代码
public class showFrm extends JFrame{
private JButton btnOK=new JButton("查看我的文件");
private JPanel mainpane=new JPanel();
public showFrm(){
this.setTitle("我的云盘");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new BorderLayout());
JPanel toppane=new JPanel();
toppane.add(btnOK);
jp.add(toppane,BorderLayout.NORTH);
jp.add(mainpane);
btnOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
btnOK_Clicked();
}
});
}
public List<String> xmlElements(String xmlDoc) {
List<String> filenames=new ArrayList<String>();
//创建一个新的字符串
StringReader read = new StringReader(xmlDoc);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
try {
//通过输入源构造一个Document
Document doc = sb.build(source);
Element root = doc.getRootElement();
System.out.println(root.getName());//输出根元素的名称(测试)
//得到根元素所有子元素的集合
List node = root.getChildren();
Element et = null;
for(int i=0;i<node.size();i++){
et = (Element) node.get(i);//循环依次得到子元素
//System.out.println(et);
//System.out.println(node.size());
String type=et.getAttributeValue("type");
if("file".equals(type))
filenames.add(et.getChild("name").getText());
String filepath=et.getChild("path").getText();
System.out.println(filepath);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return filenames;
}
private JPopupMenu initPopMenu(){
JPopupMenu menu = new JPopupMenu();
JMenuItem item1=new JMenuItem("删除");
menu.add(item1);
item3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
item1_Clicked();
}
private void item3_Clicked() {
return menu;
}
private void dispFiles(String rvalue){
List<String> filenames=xmlElements(rvalue);
mainpane.removeAll();
int WIDTH=127;
int HEIGHT=116;
for(String fname:filenames){
String imgpath="/icons/"+fname.substring(fname.lastIndexOf(".")+1).toUpperCase()+".png";
final JLabel lbl=new JLabel();
//lbl.setSize(20,20);
ImageIcon img=new ImageIcon(JLabel.class.getResource(imgpath));
img.setImage(img.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
lbl.setIcon(img);
lbl.setText(fname);
lbl.setHorizontalTextPosition(JLabel.CENTER);
lbl.setVerticalTextPosition(JLabel.BOTTOM);
final JPopupMenu menu=initPopMenu();
lbl.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
JLabel src=(JLabel)(e.getSource());
menu.show(mainpane,src.getX()+50,src.getY()+40);
}
}
});
mainpane.add(lbl);
}
mainpane.validate();
}
public String listFiles(String opttype,String path) {
String rvalue="";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/httpserver/servlet/FileServlet");
// 创建参数队列
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
formparams.add(new BasicNameValuePair("opttype", opttype));
formparams.add(new BasicNameValuePair("path", path));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
rvalue=EntityUtils.toString(entity, "UTF-8");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return rvalue;
}
private void btnOK_Clicked(){
String rvalue=listFiles("listfiles","D:\\tomcat\\apache-tomcat-6.0.35\\webapps\\httpserver\\attachment");
rvalue=rvalue.substring(0,rvalue.length()-2);
dispFiles(rvalue);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
showFrm frm=new showFrm();
frm.setSize(1200,800);
frm.setVisible(true);
}
}
问下大神怎么从xml数据流中找到label对应的值完成比如删除的操作呢?或者说删除的事件怎么写?