java 请求网上图片并保存至本地

        // 使用get请求图片,自己封装方法
        byte[] img = HttpUtils.getBytes(PATH);

        // 将字节数组转化为流
        InputStream inputStream = new ByteArrayInputStream(img);
        OutputStream os = null;
        try {
            // 图片保存的地址
            String path = "D:\\testFile\\";

            // 1K的数据缓冲
            byte[] bs = new byte[1024];

            // 读取到的数据长度
            int len;

            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }

            // 保存图片
            os = new FileOutputStream(tempFile.getPath() + File.separator + "test.jpg");

            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码将文件上传至本地的示例: ```java import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload") @MultipartConfig public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String UPLOAD_DIR = "uploads"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String applicationPath = request.getServletContext().getRealPath(""); String uploadFilePath = applicationPath + File.separator + UPLOAD_DIR; File fileUploadDirectory = new File(uploadFilePath); if (!fileUploadDirectory.exists()) { fileUploadDirectory.mkdirs(); } Part filePart = request.getPart("file"); String fileName = getFileName(filePart); String savePath = uploadFilePath + File.separator + fileName; InputStream inputStream = filePart.getInputStream(); Path filePath = Paths.get(savePath); Files.copy(inputStream, filePath); response.getWriter().println("File " + fileName + " has been uploaded successfully!"); } private String getFileName(final Part part) { final String partHeader = part.getHeader("content-disposition"); for (String content : partHeader.split(";")) { if (content.trim().startsWith("filename")) { return content.substring(content.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } } ``` 解释: - `@WebServlet("/upload")` 注释指定了上传文件的路径。 - `@MultipartConfig` 注释指定了此 Servlet 支持多部分表单数据。 - `doPost()` 方法从请求中获取上传的文件并将其保存到指定的目录中。 - `getFileName()` 方法从请求头中获取文件名。 上传表单可以使用以下 HTML: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>File Upload Form</title> </head> <body> <form method="post" action="upload" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> </body> </html> ``` 此代码将上传的文件保存到 Web 应用程序目录的 `uploads` 文件夹中。可以根据需要更改上传文件的目录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值