将图像保存到远程服务器

巴勃罗

我一直在寻找将文件保存到MySQL数据库的最佳方法,因为我有一个需要保存和检索多个图像的Java程序。我首先想到了用BLOB设置表并使用以下发现的代码:

File image = new File("C:/image.jpg");
PrepareStatement psmnt = connection.prepareStatement
("insert into save_image(image) "+ "values(?)");

FileInputStream fis = new FileInputStream(image);
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query 
insert data and image  */ 
int s = psmnt.executeUpdate();

但是,我读到,不建议将MySQL数据库用于已序列化的图像以提高性能。有人告诉我最好在服务器中保存指向目录的指针。您如何下载文件并将其上传到远程服务器?还是建议您改用MySQL数据库?

哺乳动物

您可以使用套接字将映像传输到服务器。这将是客户端/服务器通信,因此您需要发送者和接收者程序。

客户(发件人)

public static void sendFileToServer() throws Exception {
        Path path = Paths.get("path/to/file");
        byte[] data = Files.readAllBytes(path);

        // port has to be the same on client and server side
        int port = 1337; 

        //create a socket connection to the server
        Socket socket = new Socket("ipAdressOfTheServer", port);
        OutputStream out = socket.getOutputStream();

        // first write the length of the file so the receiver knows how much it has to read
        out.write(ByteBuffer.allocate(4).putInt(data.length).array());
        //write the actual file data
        out.write(data, 0, data.length);
        socket.close();
    }

服务器(接收器)

public static void receiveFile() throws Exception {
        //create a ServerSocket to listen for incoming connections
        ServerSocket server = new ServerSocket(1337);
        Socket senderSocket = server.accept();
        DataInputStream in = new DataInputStream(senderSocket.getInputStream());
        //first read the length of the file
        int lengthOfFile = in.readInt();
        byte[] buffer = new byte[lengthOfFile];

        //then read the actual file bytes
        in.readFully(buffer);

        FileOutputStream fos = new FileOutputStream("pathname");
        fos.write(buffer);
        fos.close();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将图像保存到服务器

来自分类Dev

将映像从远程服务器保存到磁盘

来自分类Dev

itextsharp - 将文件保存到远程服务器

来自分类Dev

流星将图像数据保存到服务器集合

来自分类Dev

将画布图像保存到服务器

来自分类Dev

Android将图像保存到PHP服务器

来自分类Dev

将画布中上传的图像保存到服务器

来自分类Dev

将图像上传到服务器并将URL保存到服务器

来自分类Dev

将cakeresponse保存到服务器

来自分类Dev

如何将多个远程服务器的输出保存到单个词典中?

来自分类Dev

将文件保存到远程服务器后,Yii2 Advance无法更新数据

来自分类Dev

vim在写入时保存到远程服务器

来自分类Dev

重定向输出并保存到远程服务器?

来自分类Dev

不使用express.js将图像保存到Node.js服务器

来自分类Dev

使用GridFS将视频/图像保存到NodeJS服务器中的MongoDB中

来自分类Dev

将画布图像保存到IIS服务器(MVC框架)

来自分类Dev

如何将图像从网站保存到另一台服务器

来自分类Dev

从在线打开图像,保存到服务器烧瓶

来自分类Dev

在ImageView中旋转图像并保存到服务器

来自分类Dev

C#MVC网站-将画布保存到服务器上的图像文件-图像被裁剪

来自分类Dev

OpenLayers:将添加的点保存到地理服务器

来自分类Dev

从POST请求将文件保存到Web服务器

来自分类Dev

KnpSnappyBundle将文件保存到服务器

来自分类Dev

将文件保存到流星服务器节点js

来自分类Dev

将Blob文件保存到服务器

来自分类Dev

将表单信息保存到服务器上的文件

来自分类Dev

是否可以将JSON文件保存到服务器

来自分类Dev

将Blob保存到服务器吗?

来自分类Dev

将图像保存到服务器之前,如何用event.target.files填充img src

Related 相关文章

热门标签

归档