java替换路径_JAVA-替换html中图片的路径-从html代码中提取图片路径并下载

packagecom.googosoft.until;importjava.io.BufferedInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.junit.Test;public classHtmlUtil {public staticString delHTMLTag(String htmlStr) {

String regEx_script= "

String regEx_style = "

String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式

Pattern p_script=Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);

Matcher m_script=p_script.matcher(htmlStr);

htmlStr= m_script.replaceAll(""); //过滤script标签

Pattern p_style=Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);

Matcher m_style=p_style.matcher(htmlStr);

htmlStr= m_style.replaceAll(""); //过滤style标签

Pattern p_html=Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);

Matcher m_html=p_html.matcher(htmlStr);

htmlStr= m_html.replaceAll(""); //过滤html标签

return htmlStr.trim(); //返回文本字符串

}/*** 根据图片的网络路径将图片下载到本地,并返回本地路径

*@paramurlHttp 图片的网络路径

*@parampath 新生成的图片的目录

*@return

*/

private staticString getPicture2(String urlHttp, String path) {

FileOutputStream out= null;

BufferedInputStream in= null;

HttpURLConnection connection= null;

String newPath= "";byte[] buf = new byte[1024];int len = 0;try{

URL url= newURL(urlHttp);

connection=(HttpURLConnection) url.openConnection();

connection.connect();

in= newBufferedInputStream(connection.getInputStream());

newPath= path + "/" + new Date().getTime() + ".jpg";

out= newFileOutputStream(newPath);while ((len = in.read(buf)) != -1) {

out.write(buf,0, len);

}

out.flush();

}catch(Exception e) {

e.printStackTrace();

}finally{try{

in.close();

out.close();

connection.disconnect();

}catch(IOException e) {

e.printStackTrace();

}

}returnnewPath;

}/*** 提取HTML字符串中的img列表

*@paramhtmlStr 要处理的html字符串

*@return

*/

private static ListgetImgStrList(String htmlStr) {

List list = new ArrayList<>();

String img= "";

Pattern p_image;

Matcher m_image;

String regEx_img= "]*?>";

p_image=Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);

m_image=p_image.matcher(htmlStr);while(m_image.find()) {

img=m_image.group();

Matcher m= Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);while(m.find()) {

list.add(handleSrc(m.group(1)));

}

}returnlist;

}/*** 去除src路径中的前后单引号

*@paramsrc 图片的src路径

*@return

*/

private staticString handleSrc(String src) {if (src != null) {if (src.startsWith("'")) {return src.substring(1, src.length());

}if (src.endsWith("'")) {return src.substring(0, src.length());

}

}returnsrc;

}

@Testpublic void testTransSrc() throwsException {

String str= "

标题

 
  

640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1

";

List imgList =getImgStrList(str);for(String img : imgList) {

System.out.println(getPicture2(img,"D://uploadFiles"));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: import org.dcm4che3.data.Attributes; import org.dcm4che3.data.Tag; import org.dcm4che3.io.DicomInputStream; import org.dcm4che3.util.SafeClose;import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;public class DicomUtils { public static BufferedImage getImage(String filePath) throws IOException { DicomInputStream din = null; BufferedImage bi = null; try { din = new DicomInputStream(new File(filePath)); Attributes attributes = din.readDataset(-1, -1); int rows = attributes.getInt(Tag.Rows, 0); int columns = attributes.getInt(Tag.Columns, 0); bi = new BufferedImage(columns, rows, BufferedImage.TYPE_BYTE_GRAY); attributes.getValue(Tag.PixelData, -1, -1). readBytes(bi.getRaster(), 0xffff); } finally { SafeClose.close(din); } return bi; } }答:这是使用dcm4che库,完成从dicom协议文件获取图片java代码:import org.dcm4che3.data.Attributes;import org.dcm4che3.data.Tag;import org.dcm4che3.io.DicomInputStream;import org.dcm4che3.util.SafeClose;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;public class DicomUtils {public static BufferedImage getImage(String filePath) throws IOException {DicomInputStream din = null;BufferedImage bi = null;try {din = new DicomInputStream(new File(filePath));Attributes attributes = din.readDataset(-1, -1);int rows = attributes.getInt(Tag.Rows, 0);int columns = attributes.getInt(Tag.Columns, 0);bi = new BufferedImage(columns, rows, BufferedImage.TYPE_BYTE_GRAY);attributes.getValue(Tag.PixelData, -1, -1). readBytes(bi.getRaster(), 0xffff);} finally {SafeClose.close(din);}return bi;}} ### 回答2: dcm4che是一个用于处理DICOM(数字影像和通信医疗图像的通信)协议的Java库。下面是一个简单的示例代码,用于从DICOM协议文件获取图像: ``` import org.dcm4che3.data.Attributes; import org.dcm4che3.data.Tag; import org.dcm4che3.io.DicomInputStream; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class DICOMImageReader { public static void main(String[] args) { String dicomFilePath = "/path/to/dicom/file.dcm"; try (DicomInputStream dis = new DicomInputStream(new File(dicomFilePath))) { Attributes attrs = dis.readDataset(-1, -1); // 获取图像的宽度和高度 int width = attrs.getInt(Tag.Columns, 0); int height = attrs.getInt(Tag.Rows, 0); // 获取图像数据 byte[] pixelData = attrs.getBytes(Tag.PixelData); // 创建一个BufferedImage对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY); // 将图像数据写入BufferedImage对象 image.getRaster().setDataElements(0, 0, width, height, pixelData); // 保存图像到文件 String outputFilePath = "/path/to/output/image.png"; ImageIO.write(image, "png", new File(outputFilePath)); System.out.println("图像成功保存到文件:" + outputFilePath); } catch (IOException e) { e.printStackTrace(); } } } ``` 请将`/path/to/dicom/file.dcm`替换为你实际的DICOM协议文件路径,并将`/path/to/output/image.png`替换为你想要保存图像的输出路径。这段代码将读取DICOM文件,提取图像的宽度、高度和像素数据,并将图像保存为PNG格式的文件。 ### 回答3: 使用dcm4che库从dicom协议文件获取图片Java代码如下: ``` import org.dcm4che3.data.*; import org.dcm4che3.io.*; import org.dcm4che3.util.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; public class DICOMImageExtractor { public static void main(String[] args) { String dicomFilePath = "path_to_dicom_file.dcm"; try { DicomInputStream dicomInputStream = new DicomInputStream(new FileInputStream(dicomFilePath)); DicomObject dicomObject = dicomInputStream.readDicomObject(); Attribute pixelData = dicomObject.get(Tag.PixelData); byte[] pixelDataBytes = pixelData.getByteValues(); String photometricInterpretation = dicomObject.getString(Tag.PhotometricInterpretation); int bitsAllocated = dicomObject.getInt(Tag.BitsAllocated); int rows = dicomObject.getInt(Tag.Rows); int columns = dicomObject.getInt(Tag.Columns); BufferedImage image = ImageUtil.convertToImage(pixelDataBytes, photometricInterpretation, bitsAllocated, rows, columns); String outputImagePath = "path_to_output_image.jpg"; ImageIO.write(image, "jpg", new File(outputImagePath)); System.out.println("Image extraction complete."); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码使用dcm4che库的`DicomInputStream`类读取dicom文件,并使用`DicomObject`类获取图片信息。然后,通过`ImageUtil.convertToImage`方法将获取的像素数据转换为`BufferedImage`对象。最后,使用`ImageIO.write`方法保存图片为JPEG格式。请根据实际情况根据路径填写正确的dicom文件路径和输出图片路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值