【问题标题】:Store Image In Other Server Path Using Java使用 Java 将图像存储在其他服务器路径中
【发布时间】:2020-09-15 10:39:06
【问题描述】:
public void storeEmployeeImageDetails(String folderPath, List<Employee> listEmp) {
        try {
            for (Employee emp : listEmp) {
                Blob image;
                String imageType;
                image = emp.getImageBlob();
                imageType = emp.getImageType();
                String empId = emp.getEmployeeID();

                if (!"".equals(imageType) && image != null) {
                    String type = imageType.substring(1);
                    int blobLength = (int) image.length();
                    byte[] blobAsBytes = image.getBytes(1, blobLength);
                    BufferedImage imageData = ImageIO.read(new ByteArrayInputStream(blobAsBytes));
                    ImageIO.write(imageData, type, new File(folderPath + empId + imageType));
                }
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
            logger.error("Exception Occured While Storing Employee Image In Server Folder: " + e);
        }

    }

我出错了: java.io.FileNotFoundException: https:\example.com\hr\uploads\profilepics\100.jpg(文件名、目录名或卷标语法不正确)

注意: 这里 folderPathhttps:\\example.com\\hr\\uploads\\profilepics\\ 我是从 application.properties 文件中获取的。

https:\\example.com 是一个godaddy 服务器,它是一个PHP 项目。我正在尝试将图像从 java 应用程序存储到该服务器。

【问题讨论】:

  • 从您提供的小解释来看,您似乎想将文件存储在另一台服务器上的文件系统中的某个位置。使用 http 应该是不可能的,因为如果是这样,那将是一个安全漏洞。因此,即使您能够管理它,也不应该依赖它。查看该服务器的 API 及其提供的内容(它可能接受一些路径作为查询/发布参数)。
  • 那个 URL 不是一个 REST API。这是他们服务器中的一个文件夹。有写权限。
  • 这不是关于权限,而是关于网络。
  • 这与 REST 没有任何关系。如果你想使用 HTTP,服务器需要提供一个端点/http 处理程序,它可能能够将任意 url 转换为本地路径。但是,ImageIO 无法做到这一点 - 您需要使用 url 连接(理想情况下使用 HttpClient,例如 Apache Commons Http 提供的)并将图像发布到该 url。
  • 谢谢你的想法,我会试试这个。

标签: java spring spring-boot hibernate file-io


【解决方案1】:
public class FTPUploader {
    
    FTPClient ftp = null;
    public FTPUploader(String host, String user, String pwd) throws Exception{
        ftp = new FTPClient();
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftp.connect(host);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user, pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    }
    
    
    public void uploadFile(byte[] localFileFullName, String fileName, String hostDir)
            throws Exception {
        
        try {
            InputStream input = new ByteArrayInputStream(localFileFullName);
            this.ftp.storeFile(hostDir + fileName, input);
        }catch(Exception e) {
            e.printStackTrace();
        }
        
    }

    public void disconnect(){
        if (this.ftp.isConnected()) {
            try {
                this.ftp.logout();
                this.ftp.disconnect();
            } catch (IOException f) {
                // do nothing as file is already saved to server
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多