java GUI 文件上传

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class MainInterface {

    private JFrame frame;

    public MainInterface() {
        frame = new JFrame("File Selection");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JButton button = new JButton("Select File");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showOpenDialog(frame);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    // Do something with the selected file
                    System.out.println("Selected file: " + selectedFile.getAbsolutePath());
                }
            }
        });

        frame.add(button);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws InterruptedException {
        MainInterface mainInterface = new MainInterface();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%> <%@page import="java.io.File,java.util.Date;"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>上传文件</title> </head> <body> <table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="45" align="center" valign="middle"> <form action="index.jsp?flag=1" method="post" enctype="multipart/form-data" name="form1"> <input type="file" name="file"> <input type="hidden" name="file"> <input type="submit" name="Submit" value="上传"> </form></td> </tr> </table> <% if(null != request.getParameter("flag")){ SmartUpload mySmartUpload =new SmartUpload(); long file_size_max=4000000; String fileName2="",ext="",testvar=""; String url="upload/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹) //初始化 mySmartUpload.initialize(pageContext); //只允许上载此类文件 try { mySmartUpload.setAllowedFilesList("xls");//此处的文件格式可以根据需要自己修改 //上载文件 mySmartUpload.upload(); } catch (Exception e){ %> <SCRIPT language=javascript> alert("只允许上传.xls类型文件"); </script> <% } try{ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); if (myFile.isMissing()){%> <SCRIPT language=javascript> alert("请先选择要上传的文件"); </script> <%} else{ String myFileName=myFile.getFileName(); //取得上载的文件的文件名 //out.println(myFileName); ext= myFile.getFileExt(); //取得后缀名 int file_size=myFile.getSize(); //取得文件的大小 String saveurl=""; if(file_size<file_size_max){ //更改文件名,取得当前上传时间的毫秒数值 //Calendar calendar = Calendar.getInstance(); Date date = new Date(); String filename = String.valueOf((1900+date.getYear()))+String.valueOf((date.getMonth()+1)); saveurl=application.getRealPath("/")+url; File file = new File(saveurl); if(!file.exists()){ file.mkdirs(); } saveurl+=filename+"."+ext; //保存路径 myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL); String ret = "alert('上传成功!');"; out.print("<script language=javascript>" + ret + "</script>"); } } }catch (Exception e){ out.print(e.toString()+"错误信息......................!"); } } %> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值