Struts2上传图片到服务器之后端

好久没有写点什么东西了,今天来写点上传图片到服务器的东西吧

    public void upload() throws IOException{
        
/*        
        try {
//            filePath = nfsUtils.uploadFile(importFile,fileType);
            String ftpUrl = getText("upload.url");//
            int ftpPort = Integer.parseInt(getText("upload.port"));
            String ftpUser = getText("upload.userName");//
            String ftpPasswd = getText("upload.password");//
            String path=getText("upload.path");
            String download = getText("http.download.url");//
            String smallFileName = FtpUtils.uploadSmallImgToFtp(importFile, fileType, ftpUrl, ftpPort, ftpUser, ftpPasswd,path);
            setFilePath(download+path+smallFileName);
            
        }    
        catch (Exception e) {
            e.printStackTrace();
        }
        json.put("state", "1");
        ow.write(json.toString());

//        System.out.println("jsonObject直接创建json:");
        return "init";
//    */    
        
        try {
//            filePath = nfsUtils.uploadFile(importFile,fileType);
            String ftpUrl = getText("upload.url");//
            int ftpPort = Integer.parseInt(getText("upload.port"));
            String ftpUser = getText("upload.userName");//
            String ftpPasswd = getText("upload.password");//
            String path=getText("upload.path");
            String download = getText("http.download.url");//
            String smallFileName = FtpUtils.uploadSmallImgToFtp(importFile, fileType, ftpUrl, ftpPort, ftpUser, ftpPasswd,path);
            setFilePath(download+path+"93d78ec2-ab76-41be-9ebd-816e3fe26303.png");
            
        }    
        catch (Exception e) {
            e.printStackTrace();
        }
        
        HttpServletResponse response=ServletActionContext.getResponse();
        //以下代码从JSON.java中拷过来的
        response.setContentType("text/html");
        PrintWriter out;
        out = response.getWriter();
        //将要被返回到客户端的对象
      
        JSONObject json=new JSONObject();
        json.accumulate("success", true);
        json.accumulate("filePath", filePath);
        out.println(json.toString());
//      因为JSON数据在传递过程中是以普通字符串形式传递的,所以我们也可以手动拼接符合JSON语法规范的字符串输出到客户端
//      以下这两句的作用与38-46行代码的作用是一样的,将向客户端返回一个User对象,和一个success字段
//      String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONActionGeneral\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
//      out.println(jsonString);
        out.flush();
        out.close();
    }

这是两个返回值,第一个是返回配置的页面,第二个由于前台要去回传一个json传so那就返回一个吧

然后勒

 

    /**
     * 上传缩略图至FTP服务器
     * @param f
     * @return 重新生成的文件名
     */
    public static String uploadSmallImgToFtp(File f,String fileType,String ftpUrl,int ftpPort,String ftpUser,String ftpPasswd,String path){
        if(null == f || null == fileType) return "";
//        String[] temp = fileName.split(".");
//        String extFileName = temp[temp.length-1];
        String guidFileName = GUID.getGUID() + "." + fileType;
        try {  
            FileInputStream in=new FileInputStream(f);  
            boolean flag = FtpUtils.uploadFile(ftpUrl, ftpPort, ftpUser, ftpPasswd, path, guidFileName, in);  
            System.out.println(flag);  
            return guidFileName;
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
        return guidFileName;
    }

/**
    * Description:        向FTP服务器上传文件
    * @param url          FTP服务器hostname
    * @param port         FTP服务器端口
    * @param username     FTP登录账号
    * @param password     FTP登录密码
    * @param path         FTP服务器保存目录
    * @param filename     上传到FTP服务器上的文件名
    * @param input        输入流
    * @return             成功返回true,否则返回false
    */
    public static boolean uploadFile(String url, int port, String username,
        String password, String path, String filename, InputStream input) {
        // 初始表示上传失败
        boolean success = false;
        // 创建FTPClient对象
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("GBK");
        try {
            int reply;
            // 连接FTP服务器
            // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.connect(url, port);
            // 登录ftp
            if (ftp.login(username, password)){
                FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
                conf.setServerLanguageCode("zh");
                ftp.configure(conf);
                // 看返回的值是不是230,如果是,表示登陆成功
                reply = ftp.getReplyCode();
                // 以2开头的返回值就会为真
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return success;
                }
                //设置被动模式
                ftp.enterLocalPassiveMode();
                //设置以二进制方式传输  
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
                // 转到指定上传目录
                success = ftp.changeWorkingDirectory(path);
                if(!success){
                    return false;
                }
                // 将上传文件存储到指定目录
                success = ftp.storeFile(filename, input);
                if(!success){
                    return false;
                }
                // 关闭输入流
                input.close();
                // 退出ftp
                ftp.logout();
                // 表示上传成功
                success = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception wa) {
            wa.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值