「java上传文件并读取」java实现文件上传的三种方式

博主:adminadmin 2022-11-29 02:42:07 50

本篇文章给大家谈谈java上传文件并读取,以及java实现文件上传的三种方式对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么读取文件夹中的内容并上传

FileInputStream fis = new FileInputStream("d:/a.txt");//从a.txt中读出

FileOutputStream fos = new FileOutputStream("d:/b.txt");//写到b.txt中去

BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));

String temp;

while((temp = reader.readLine())!= null){//一次读一行

write.write(temp);

}

reader.close();

write.close();

java项目中文件的上传与读取

互联网项目一般会有单独的服务器存放静态资源,图片就是一种静态资源,在这里就是区别于项目部署的另一台服务器。这时候你项目里面都是使用相对路径,像你上面所说的常量/opt/upload/这种做法是正确的,上传图片的时候,常见的有使用日期分目录存储的,如/opt/upload/2014/11/03/***.jpg,对于图片的路径,数据库里一般来说保存到2014/11/03/***.jpg就可以了。这是存图片。

取图片,一般来说资源都必须发布服务才能让外网访问。例如,你可以在你项目中写个servlet用来读取图片,如下面的服务地址***.jpg,其中2014前面的路径是固定的,后面的是你数据库里存储的图片地址,这时你页面代码里面只需固定前缀 + 图片相对地址则可将图片读出来。

上面这种方法用的其实比较少,一般来说静态服务器都会部署一个web容器,然后使用单独的域名,打个比方,你在静态服务器上有个tomcat,目录/opt/tomcat/webapp/staticprojectname,staticprojectname是工程名,然后在上传的所有图片在staticprojectname下面,例如/opt/tomcat/webapp/staticprojectname/2014/11/03/***.jpg,然后在你原工程里面直接使用http://静态服务ip:port/staticprojectname/2014/11/03/***.jpg就可以访问到图片了,同样的在你代码里面2014前面的地址是固定的,配置成常量,后面的则是数据库里存的图片相对地址。

说了这么多,有点乱,希望你能明白

java上传txt文件后读取内容

告诉你很麻烦

FormFile upfile = fileupForm.getFilecontext();// 获取客户端选中的文件实体

FileReader fr = new FileReader(upfile);

BufferedReader br = new BufferedReader(fr);

String line="";

int lineNum=0;

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

System.out.println(line);

lineNum++;

if(lineNum==N) break; //当读取的行数为自定义的行数N时break.退出

}

br.close();

fr.close();

java web开发,上传图片并读取

java web开发中,使用文件操作类来上传图片并读取,如下代码:

 * @desc: 图片处理工具

 * @author: bingye

 * @createTime: 2015-3-17 下午04:25:32

 * @version: v1.0

 */

public class ImageUtil {

 

    /**

    * 将图片写到客户端

    * @author: bingye

    * @createTime: 2015-3-17 下午04:36:04

    * @history:

    * @param image

    * @param response void

    */

    public static void writeImage(byte[] image,HttpServletResponse response){

        if(image==null){

            return;

        }

        byte[] buffer=new byte[1024];

        InputStream is=null;

        OutputStream os=null;

        try {

            is=new ByteArrayInputStream(image);

            os=response.getOutputStream();

            while(is.read(buffer)!=-1){

                os.write(buffer);

                os.flush();

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally{

            try {

                if(is!=null){is.close();}

                if(os!=null){os.close();}

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

     

    /**

    * 获取指定路劲图片

    * @author: bingye

    * @createTime: 2015-3-21 上午10:50:44

    * @param filePath

    * @param response void

    */

    public static void writeImage(String filePath,HttpServletResponse response){

        File imageFile=new File(filePath); 

        if(imageFile!=null  imageFile.exists()){

            byte[] buffer=new byte[1024];

            InputStream is=null;

            OutputStream os=null;

            try {

                is=new FileInputStream(imageFile);

                os=response.getOutputStream();

                while(is.read(buffer)!=-1){

                    os.write(buffer);

                    os.flush();

                }

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            } finally{

                try {

                    if(is!=null){is.close();}

                    if(os!=null){os.close();}

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

    }

     

    /**

    * 图片上传到文件夹

    * @author: bingye

    * @createTime: 2015-3-20 下午08:07:25

    * @param file

    * @param savePath

    * @return boolean

    */

    public static ResultDto uploadToLocal(CommonsMultipartFile file,String savePath){

        if(file!=null  !file.isEmpty()){

            //获取文件名称

            String fileName=file.getOriginalFilename();

            //获取后缀名

            String suffixName=fileName.substring(fileName.indexOf(".")+1);

            //新名称

            String newFileName=System.currentTimeMillis()+"."+suffixName;

            //新文件路劲

            String filePath=savePath+newFileName;

            //获取存储文件路径

            File fileDir=new File(savePath);

            if(!fileDir.exists()){

                //如果文件夹没有:新建

                fileDir.mkdirs();

            }

            FileOutputStream fos=null;

            try {

                fos=new FileOutputStream(filePath);

                fos.write(file.getBytes());

                fos.flush();

                return ResultUtil.success("UPLOAD_SUCCESS", URLEncoder.encode(newFileName,"utf-8"));

            } catch (Exception e) {

                e.printStackTrace();

                return ResultUtil.fail("UPLOAD_ERROR");

            } finally{

                try {

                    if(fos!=null){

                        fos.close();

                    }

                } catch (IOException e) {

                    e.printStackTrace();

                    return ResultUtil.fail("UPLOAD_ERROR");

                }

            }

        }

        return ResultUtil.fail("UPLOAD_ERROR");

    }

     

     

     

}

关于java上传文件并读取和java实现文件上传的三种方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。