这是一段将PDF文件输出到浏览器的代码,它可以更快吗?
这是在Java servlet中实现的.
private ByteArrayOutputStream getByteArrayOutputStreamFromFile(File file) throws Exception {
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new ByteArrayOutputStream();
byte[] byteContent = new byte[1024 * 1024];
int len = 0;
while ((len = bis.read(byteContent)) != -1) {
bos.write(byteContent, 0, len);
}
return bos;
} catch (Exception ex) {
throw ex;
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用方法(信用到神元路):
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
正是:
org.apache.commons.io.FileUtils.copyURLToFile(driver.getCurrentUrl(), "C:\\Users\\myDocs\\myfolder\myFile.pdf");
但是,Eclipse强调copyURLToFile并建议使用copyFile.当我切换到copyFile,它建议copyURLToFile再次使用???
我一直在尝试通过java从url下载图像.
我尝试用很多方法解决它,但我没有成功.
我正在写自己的方式,希望有人能找到错误的地方:
使用第一种和第二种方法,在我尝试打开图像时出现以下错误:
...文件似乎已损坏,损坏或太大
......它的尺寸小于应有的尺寸.
我想这与编码有关.
第一种方式:
URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
InputStream in = url.openStream();
Files.copy(in, Paths.get("someFile.jpg"), StandardCopyOption.REPLACE_EXISTING);
in.close();
Run Code Online (Sandbox Code Playgroud)
第二种方式:
File f= new File("c:\\image.jpg");
URL myUrl = new URL("http://www.avajava.com/images/avajavalogo.jpg");
FileUtils.copyURLToFile(myUrl, f);
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我得到了Exception thread "main" java.lang.IllegalArgumentException: image == null!
第三种方式:
URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);
Run Code Online (Sandbox Code Playgroud)
我真的需要你的帮助!!!很久以前我一直试图解决它而没有成功.
提前致谢!!!
我想识别Java套接字中的数据流结束.当我运行下面的代码时,它只是卡住并继续运行(它停留在值10).
我也希望程序下载二进制文件,但最后一个字节总是不同的,所以我不知道如何停止while(实用).
String host = "example.com";
String path = "/";
Socket connection = new Socket(host, 80);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.write("GET "+ path +" HTTP/1.1\r\nHost: "+ host +"\r\n\r\n");
out.flush();
int dataBuffer;
while ((dataBuffer = connection.getInputStream().read()) != -1)
System.out.println(dataBuffer);
out.close();
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何提示.
最后,我的最终目标是:
使用以下方法的目标是获取byte[]可在下游用作电子邮件附件的目标(以避免写入磁盘):
public byte[] retrievePDF() {
HttpClient httpClient = new HttpClient();
GetMethod httpGet = new GetMethod("http://website/document.pdf");
httpClient.executeMethod(httpGet);
InputStream is = httpGet.getResponseBodyAsStream();
byte[] byteArray = new byte[(int) httpGet.getResponseContentLength()];
is.read(byteArray, 0, byteArray.length);
return byteArray;
}
Run Code Online (Sandbox Code Playgroud)
对于特定PDF,该getResponseContentLength()方法返回101,689作为长度.的奇怪的是,如果设置了一个断点和询问byteArray变量,它具有101689个字节元素,但是,之后字节#3744的阵列的剩余字节是全零(0). 因此,PDF阅读器客户端(如Adobe Reader)无法读取生成的PDF.
为什么会这样?
通过浏览器检索相同的PDF并保存到磁盘,或者使用如下方法(我在回答此StackOverflow帖子后模式化),得到可读的PDF:
public void retrievePDF() {
FileOutputStream fos = null;
URL url;
ReadableByteChannel rbc = null;
url = new URL("http://website/document.pdf");
DataSource urlDataSource = new URLDataSource(url);
/* …Run Code Online (Sandbox Code Playgroud) 有没有办法在java swing上创建下载按钮?我尝试在互联网上搜索答案,但我只获得了如何在jsp/servlet上创建下载链接的链接
我正在读取URL的内容并写一个文件,问题是我无法写入文件中的所有内容,也不知道我做错了什么.
我的代码,
try {
URL url = new URL(sourceUri);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
file.getParentFile().mkdirs();
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine + System.getProperty("line.separator"));
}
br.close();
System.out.println("DONE");
}catch (IOException ioe){
ioe.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
return ontologies;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙