java上传文件,报错信息:Streamendedunexpectedly - 爱问答

(爱问答)

java上传文件,报错信息:Streamendedunexpectedly

Controller处理方法:

java 上传文件,报错信息:Stream ended unexpectedly

报错信息:

java 上传文件,报错信息:Stream ended unexpectedly

java 上传文件,报错信息:Stream ended unexpectedly

String res = "";         HttpURLConnection conn = null;         String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符         try{            File f=new File("G:1.jpg");                         URL url = new URL("http://192.168.21.148:8080/APPSERVER/upload/");            conn = (HttpURLConnection) url.openConnection();            conn.setConnectTimeout(50000);            conn.setReadTimeout(30000);            conn.setDoOutput(true);            conn.setDoInput(true);            conn.setUseCaches(false);            conn.setRequestMethod("POST");            conn.setRequestProperty("Connection", "Keep-Alive");            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 firefox/26.0");            conn.setRequestProperty("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");            conn.setRequestProperty("Accept-Encoding","gzip, deflate");            conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");            conn.setRequestProperty("Content-Type","multipart/form-data; boundary="+BOUNDARY);            OutputStream out = new DataOutputStream(conn.getOutputStream());              byte[] fb = new byte[(int) f.length()];            FileInputStream fis = new FileInputStream(f);            fis.read(fb);                         StringBuffer strBuf = new StringBuffer();            String inputName="uploadfile";            String filename="1.jpg";            String contentType="image/jpeg";            strBuf.append(" ").append("--").append(BOUNDARY).append(" ");            strBuf.append("Content-Disposition: form-data; name=""+inputName+""; filename=""+filename+"" ");            strBuf.append("Content-Type:" + contentType + " ");            out.write(strBuf.toString().getBytes());            out.flush();            out.write(fb);            out.flush();            out.write(("--"+BOUNDARY+"--").getBytes());             out.flush();            out.close();                         strBuf = new StringBuffer();             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));             String line = null;             while ((line = reader.readLine()) != null) {                 strBuf.append(line).append(" ");             }             System.out.println(strBuf.toString());                     }catch(Exception e){            e.printStackTrace();        }


这是说上载的文件中断,可能是网络连接的问题吧,或者是网页在上载过程中刷新了,必须网络稳定才可以完成,如果你的文件很大,建议将它切割成小块,比如用WinZip

相关标签:java

下一篇:Word07文本结构很乱

上一篇:servlet文件上传是不是每一个属性都要在servlet中进行封装

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?