java 生成url,使用java创建URL

Hii to all

I m trying to create a url using java. Address is http://www.merchant.com/pg/index.html

When i m going to run this address on browser i got error 404 page not found. There is a

index.html file in my project.

My code ispublic class Url {

/**

* @param args

*/

public static void main(String[] args) {

try {

//

// Creating a url object by specifing each parameter separately, including

// the protocol, hostname, port number, and the page name

//

URL url = new URL("http", "www.Merchant.com" , 80, "/pg/index.html");

//URL url = new URL("http://Merchant.com/index.html");

// We can also specify the address in a single line.

//www.merchant.com/pg/index.html

BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

String line;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

reader.close();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

How to solve this issue? Plz help.

解决方案

I don't think the problem is in your URL definition. After I checked the url you said, it in fact has a customized page for the HTTP Error 404, which means resource not found. However, the url http://www.merchant.com/index.html is valid and should give you the expected result.

[Explanation]

What it means is; You are trying to retrieve content from a file that does not exist in the destination URL. In order to read the URL successfully, you must first create a file in the directory named pg of your hosting server. Then in this server, copy your file or create a file that you want to be accessible. Otherwise, you will have to use a valid location to retrieve the file content.

N.B. If you the url does not point to an existing file, you will get a file not found exception...which I suppose is what you're getting in your stack trace.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用第三方库生成 URL 的二维码,其中比较流行的是 zxing(Zebra Crossing)库。下面是使用 zxing 库生成 URL 二维码的示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String url = "https://www.example.com"; int width = 300; int height = 300; String format = "png"; String fileName = "qrcode.png"; Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix; try { matrix = writer.encode(url, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix); ImageIO.write(image, format, new File(fileName)); System.out.println("QR code generated successfully."); } catch (WriterException e) { System.out.println("Error generating QR code: " + e.getMessage()); } catch (IOException e) { System.out.println("Error saving QR code: " + e.getMessage()); } } } ``` 在上面的示例代码中,我们使用 `QRCodeWriter` 类将 URL 编码为二维码的位矩阵,然后使用 `MatrixToImageWriter` 类将位矩阵转换为 `BufferedImage` 对象。最后我们将 `BufferedImage` 对象保存为 PNG 图像文件。你可以根据需要调整代码中的 URL、图片宽度、图片高度、图片格式和文件名等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值