Java图片上传代码


001package com.bj95ol.test;
002   
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.InputStream;
009import java.io.InputStreamReader;
010import java.io.OutputStreamWriter;
011import java.io.PrintWriter;
012import java.net.HttpURLConnection;
013import java.net.MalformedURLException;
014import java.net.URL;
015   
016import javax.servlet.ServletInputStream;
017import javax.servlet.http.HttpServletRequest;
018   
019import sun.misc.BASE64Decoder;
020import sun.misc.BASE64Encoder;
021   
022public class SendImgToWeb {
023    /**
024     * 获得网络图片地址。或者图片地址
025     * @param url
026     * @return
027     */
028    public String getContentFromWeb(String url)
029    {
030        String filecontent="";
031        InputStream is=null;
032        BASE64Encoder base=new BASE64Encoder();
033        if(url.startsWith("http"))
034        {
035            try {
036                HttpURLConnection urlconn=(HttpURLConnection)new URL(url).openConnection();
037                is=urlconn.getInputStream();
038            } catch (MalformedURLException e) {
039                // TODO Auto-generated catch block
040                e.printStackTrace();
041            } catch (IOException e) {
042                // TODO Auto-generated catch block
043                e.printStackTrace();
044            }
045        }
046        int n=0;
047        byte[] b=null;
048        try {
049            while((n=is.available())>0)
050            {
051                n=is.read(b);
052                if(n==-1)break;
053                filecontent=filecontent+base.encode(b);
054                   
055            }
056            is.close();
057        } catch (IOException e) {
058            // TODO Auto-generated catch block
059            e.printStackTrace();
060        }
061        return filecontent;
062    }
063       
064    /**
065     * 将图片内容用post方式发送到url中
066     * @param url
067     * @param postcontent
068     */
069       
070    public void sendImgbyPost(String url,String postcontent)
071    {
072        try {
073            HttpURLConnection huc=(HttpURLConnection)new URL(url).openConnection();
074            huc.setDoInput(true);
075            huc.setDoOutput(true);
076            huc.setRequestMethod("POST");
077               
078            PrintWriter pw=new PrintWriter(new OutputStreamWriter(huc.getOutputStream()));
079            pw.print(postcontent);
080            pw.close();
081               
082            BufferedReader br=new BufferedReader(new InputStreamReader(huc.getInputStream()));
083            String content="";
084            String line=br.readLine();
085            while(line!=null)
086            {
087                content=content+line;
088                line=br.readLine();
089                   
090            }
091               
092        } catch (MalformedURLException e) {
093            // TODO Auto-generated catch block
094            e.printStackTrace();
095        } catch (IOException e) {
096            // TODO Auto-generated catch block
097            e.printStackTrace();
098        }
099           
100    }
101    /**
102     * 在服务器端获取发送过来的内容
103     * @param request
104     * @return
105     */
106    public String receiveContent(HttpServletRequest request)
107    {
108        int a = 0;
109         byte[] b = new byte[4096];
110        String result="";
111        try
112        {
113         ServletInputStream sis=request.getInputStream();
114        int line=sis.readLine(b, 0, b.length);
115        while(line!=-1)
116        {
117        result=result+new String(b,0,line);
118        line=sis.readLine(b, 0, b.length);
119        }
120        }
121        catch(Exception e)
122        {
123        e.printStackTrace();
124        }
125        return  result;
126    }
127       
128    /**
129     * 将接受过来的信息生成文件
130     * @param request
131     * @param filename
132     */
133    public void createFile(HttpServletRequest request,String filename)
134    {
135        File file=new File(filename);
136        try {
137            FileOutputStream fos=new FileOutputStream(file);
138            String content=receiveContent(request);
139            BASE64Decoder base=new BASE64Decoder();
140            byte[] b=base.decodeBuffer(content);
141            fos.write(b);
142            fos.close();
143               
144        } catch (FileNotFoundException e) {
145            // TODO Auto-generated catch block
146            e.printStackTrace();
147        } catch (IOException e) {
148            // TODO Auto-generated catch block
149            e.printStackTrace();
150        }
151           
152    }
153   
154}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值