上传图片并保存到服务器上之后,如何得到该图片服务器路径(http://~)

1. 情景描述

2. 测试代码


 

1. 情景描述

 

首先使用asp.net中自带空间FileUpload将图片上传到服务器上之后,在“在线编辑器”中需要显示该图片,需要得到该图片的以http://开头的url路径

 


2. 测试代码

 

其实只需要使用 System.Web.UI.Control.ResolveUrl (string),其中string参数是上传图片的相对路径。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用Apache PDFBox和Spring Boot的MultipartFile类来实现PDF图片传到服务器保存路径到数据库。首先,使用MultipartFile类将PDF文件上传到服务器上的指定目录。然后,使用Apache PDFBox将PDF文件转换为图片,并将图片保存服务器上的指定目录。最后,将图片路径保存到数据库中。以下是示例代码: ``` @PostMapping("/uploadPDF") public String uploadPDF(@RequestParam("file") MultipartFile file) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String uploadDir = "pdf_uploads"; String filePath = uploadDir + "/" + fileName; try { FileUploadUtil.saveFile(uploadDir, fileName, file); PDDocument document = PDDocument.load(new File(filePath)); PDFRenderer pdfRenderer = new PDFRenderer(document); BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB); String imageDir = "image_uploads"; String imageName = FilenameUtils.getBaseName(fileName) + ".png"; String imagePath = imageDir + "/" + imageName; FileUploadUtil.saveImage(imageDir, imageName, bim); document.close(); // 将图片路径保存到数据库中 // ... return "File uploaded successfully!"; } catch (IOException e) { e.printStackTrace(); return "Failed to upload file!"; } } ``` 其中,FileUploadUtil是一个工具类,用于保存文件和图片到指定目录: ``` public class FileUploadUtil { public static void saveFile(String uploadDir, String fileName, MultipartFile file) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } try (InputStream inputStream = file.getInputStream()) { Path filePath = uploadPath.resolve(fileName); Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new IOException("Could not save file: " + fileName, e); } } public static void saveImage(String uploadDir, String imageName, BufferedImage image) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } Path imagePath = uploadPath.resolve(imageName); ImageIO.write(image, "png", imagePath.toFile()); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值