
正文
java实现下载图片代码 java图片下载功能
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java代码,里面有下载图片的代码,new File(getClass().getResource("").getFile().toString())
是放在 /home/image 这个目录下面, 但是这个路径是非root用户的根目录,可能会出现问题, 所以建议放在 /data/ 这个目录下面, 在这下面创建一个image目录存放文件。
相关问答
Q1: Java文件下载怎么实现的
下载就很简单了
把你要下载的文件做成超级链接,可以不用任何组件
比如说
下载一个word文档
a href="名称.doc"名称.doc/a
路径你自己写
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.util.Random;
/**
*
* 实现了下载的功能*/
public class SimpleTh {
public static void main(String[] args){
// TODO Auto-generated method stub
//String path = "倩女幽魂.mp3";//MP3下载的地址
String path ="";
try {
new SimpleTh().download(path, 3); //对象调用下载的方法
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getFilename(String path){//获得文件的名字
return path.substring(path.lastIndexOf('/')+1);
}
public void download(String path,int threadsize) throws Exception//下载的方法
{//参数 下载地址,线程数量
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();//获取HttpURLConnection对象
conn.setRequestMethod("GET");//设置请求格式,这里是GET格式
conn.setReadTimeout(5*1000);//
int filelength = conn.getContentLength();//获取要下载文件的长度
String filename = getFilename(path);
File saveFile = new File(filename);
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.setLength(filelength);
accessFile.close();
int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1;
for(int threadid = 0;threadid=threadsize;threadid++){
new DownloadThread(url,saveFile,block,threadid).start();
}
}
private final class DownloadThread extends Thread{
private URL url;
private File saveFile;
private int block;//每条线程下载的长度
private int threadid;//线程id
public DownloadThread(URL url,File saveFile,int block,int threadid){
this.url = url;
this.saveFile= saveFile;
this.block = block;
this.threadid = threadid;
}
@Override
public void run() {
//计算开始位置的公式:线程id*每条线程下载的数据长度=?
//计算结束位置的公式:(线程id+1)*每条线程下载数据长度-1=?
int startposition = threadid*block;
int endposition = (threadid+1)*block-1;
try {
try {
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.seek(startposition);//设置从什么位置写入数据
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5*1000);
conn.setRequestProperty("Range","bytes= "+startposition+"-"+endposition);
InputStream inStream = conn.getInputStream();
byte[]buffer = new byte[1024];
int len = 0;
while((len = inStream.read(buffer))!=-1){
accessFile.write(buffer, 0, len);
}
inStream.close();
accessFile.close();
System.out.println("线程id:"+threadid+"下载完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Q2: java如何从数据库中下载以二进制存储的图片
/**
* Created by IntelliJ IDEA.
* User: ljt
* Date: 2003-3-31
* Time: 18:51:38
* To change this template use Options | File Templates.
*/
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Clob;
public class TestOpenDoc {
public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //**PreparedStatement用
public Connection conn = null;
public Statement stmt = null;
public TestOpenDoc() {
}
public boolean getConnect() {
//这是我的数据库所在
String serverName = "prosrv";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = DriverManager.getConnection(url, "appuser", "appuser");
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!test.getConnect()) {
System.out.println("数据库连结错误");
return ;
}
try{
test.conn.setAutoCommit(false);
byte a[] = null; //**将测试文件test.doc读入此字节数组
java.io.FileInputStream fin = null;
java.io.FileOutputStream fout = null;
//Oracle提供的
try {
java.io.File f1 = new java.io.File("c:/test.doc");
java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写
//入该文 件,和源文件对比测试用
fin = new java.io.FileInputStream(f1);
fout = new java.io.FileOutputStream(f2);
int flength = (int) f1.length(); //**读入文件的字节长度
System.out.println("file length::" + flength);
a = new byte[flength];
int i = 0;
int itotal = 0;
//* 将文件读入字节数组
for (; itotal flength; itotal = i + itotal) {
i = fin.read(a, itotal, flength - itotal);
}
fin.close();
System.out.println("read itotal::" + itotal);
//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
opst.setInt(2, flength);
opst.executeUpdate();
opst.clearParameters();
// /**插入其它数据后,定位BLOB字段
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) opst.executeQuery();
if (ors.next()) {
oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段
int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段
System.out.println("j:" + j);
test.conn.commit();
ors.close();
Clob clob;
clob = ors.getClob("");
String str;
str = clob.toString();
str = clob.getSubString(0L,(int)clob.length());
System.out.println(str);
}
System.out.println("insert into ok");
byte b[] = null; //**保存从BLOB读出的字节
opst.clearParameters();
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
ors = (OracleResultSet) opst.executeQuery();
if (ors.next()) {
oracle.sql.BLOB blob2 = ors.getBLOB(1);
System.out.println("blob2 length:" + blob2.length());
b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据
System.out.println("b length::" + b.length);
test.conn.commit();
}
ors.close();
// 将从BLOB读出的字节写入文件
fout.write(b, 0, b.length);
fout.close();
System.out.println("write itotal::" + b.length);
}
catch (Exception e) {
System.out.println("errror :" + e.toString());
e.printStackTrace();
}
finally { //**关闭所有数据联接
test.conn.commit();
}
}
catch(Exception e){
System.out.println(e);
}
}
}
Q3: Java编写一下图片下载程序?
楼上的写的没错java实现下载图片代码,不过感觉太麻烦了java实现下载图片代码,用hutool工具包来写个方法
HttpUtil.downloadFile("", new File("F://demo4/baidu_logo.png"));
第一个参数为百度logo图片,第二个为我本地下载位置,下载结果如图
Q4: 下载图片 可以弹出选择路径 然后下载到指定路径用Java怎么做?
方便起见你就按照我的目录一步一步来吧
1.建立MANIFEST.MF (在D:\abc目录下)
以下内容为文件内容 (只复制2行等于号之间的)
==================
Manifest-Version: 1.0
Main-Class: RoundButton
Created-By: Abc
==================
2.打包
把你的RoundButton.class复制到D:\abc目录下
然后
开始--》运行--》敲cmd打开命令行,
输入cd d:\abc 回车
输入d: 回车
输入jar cvfm abc.jar MANIFEST.MF *.* 回车
然后就能看到abc目录下有个叫abc.jar的文件,双击可以运行,jar包名字随便改,不影响
以上操作前提条件是你机器里jdk环境变量配置正确,100%可用,我已经试过
Q5: 如何用java代码获取 这个网页上的图片
你也没说是要把图片下载下来还是什么的
这里我实现的是将图片下载到电脑
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownLoad {
public static void main(String[] args) {
File dstFile = new File("E:/test/test/test.jpg");
try {
URL url = new URL("");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
saveFile(is, dstFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveFile(InputStream is,File dstFile){
FileOutputStream fos = null;
File parentFile = dstFile.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
try {
fos = new FileOutputStream(dstFile);
byte[] buff = new byte[1024 * 4];
int len;
while((len = is.read(buff)) != -1){
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(is != null){
is.close();
}
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
java实现下载图片代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java图片下载功能、java实现下载图片代码的信息别忘了在本站进行查找喔。







