栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java applet上传文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java applet上传文件

这是一些代码,可能会帮助您,它来自我的一个旧项目,其中删除了许多无关的内容,以物有所值。基本上,我认为您问题中的代码缺少HTTP协议要求的某些部分

public class UploaderExample{    private static final String Boundary = "--7d021a37605f0";    public void upload(URL url, List<File> files) throws Exception    {        HttpURLConnection theUrlConnection = (HttpURLConnection) url.openConnection();        theUrlConnection.setDoOutput(true);        theUrlConnection.setDoInput(true);        theUrlConnection.setUseCaches(false);        theUrlConnection.setChunkedStreamingMode(1024);        theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="     + Boundary);        DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());        for (int i = 0; i < files.size(); i++)        { File f = files.get(i); String str = "--" + Boundary + "rn" + "Content-Disposition: form-data;name="file" + i + ""; filename="" + f.getName() + ""rn" + "Content-Type: image/pngrn" + "rn"; httpOut.write(str.getBytes()); FileInputStream uploadFileReader = new FileInputStream(f); int numBytesToRead = 1024; int availableBytesToRead; while ((availableBytesToRead = uploadFileReader.available()) > 0) {     byte[] bufferBytesRead;     bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]  : new byte[availableBytesToRead];     uploadFileReader.read(bufferBytesRead);     httpOut.write(bufferBytesRead);     httpOut.flush(); } httpOut.write(("--" + Boundary + "--rn").getBytes());        }        httpOut.write(("--" + Boundary + "--rn").getBytes());        httpOut.flush();        httpOut.close();        // read & parse the response        InputStream is = theUrlConnection.getInputStream();        StringBuilder response = new StringBuilder();        byte[] respBuffer = new byte[4096];        while (is.read(respBuffer) >= 0)        { response.append(new String(respBuffer).trim());        }        is.close();        System.out.println(response.toString());    }    public static void main(String[] args) throws Exception    {        List<File> list = new ArrayList<File>();        list.add(new File("C:\square.png"));        list.add(new File("C:\narrow.png"));        UploaderExample uploader = new UploaderExample();        uploader.upload(new URL("http://systemout.com/upload.php"), list);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号