300分赠送:为什么我的bean只能上传文本文件,上传二进制exe或图片就不行! File file = new File("out.txt");你的这句有问题,最好传个变量进去,标明类型,否则都是txt文件。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把这个out.txt改成out.exe或者out.gif等等,是什么类型就改成什么后缀。 对不起:程序发错啦!package sm.net.upload;import java.util.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.ServletInputStream;public class upload { String me = "sm.net.upload.upload"; int filecount = 0; //总共上传文件数量 int currentNo = 0; //当前上传文件编号 String division = ""; //分割字符 例如:----------7d32bf24409de File file; FileOutputStream fos; ServletInputStream svins; byte[] readByte = new byte[4096]; //输入流缓冲区 int readCount; //readCount 记录从输入流中实际读取的字符数 String line; //行 String name,filename, targetpath; //上传名称,文件名称,目标路径, int key, len, index; //返回值,字符长度,index位置 long cx = 0; //循环器 Hashtable ht = null;//上传的其他非文件值//---------------------------------------------------------------------------------- public upload() { targetpath = ""; readCount = 0; ht = new Hashtable(); }//---------------------doUpload------------------------------------public void doUpload(HttpServletRequest req) throws ServletException,IOException{ String fileinfo,No;//文件信息,编号 boolean isFile; show("开始上传"); try{ svins = req.getInputStream(); }catch(Exception e){ show("初始化servletInputStream失败"); svins.close(); return; } //读取第一行 readCount = svins.readLine(readByte, 0, readByte.length); line = new String(readByte);line = line.trim(); //得到分割符 division = line; show("分割符 division='"+division+"'"); //开始循环读取数据 while(readCount != -1){//while1 //一个节点的分析与写入 if (line.startsWith(division) ){//if1 cx =cx +1;No = String.valueOf(cx); show("----发现节点分割符---"+No); //下读一行 readCount = svins.readLine(readByte, 0, readByte.length); line = new String(readByte);line = line.trim(); //确定是否为文件 if (line.startsWith("content-")&&(line.indexOf("name=")!=-1)){//if2 show("开始分析文件头"+No); if (line.indexOf("filename=")!=-1){//if3 //是文件 得到文件名称 isFile = true; index = line.lastIndexOf("\\"); filename = line.substring(index+1); filename = filename.substring(0,filename.length()-1); if (filename.indexOf('"') !=-1){ index = filename.indexOf('"'); filename = filename.substring(0,index); } show("filename:"+filename+No); //下读二行------------- readCount = svins.readLine(readByte, 0, readByte.length); readCount = svins.readLine(readByte, 0, readByte.length); //写文件--------------- try{ file = new File(filename); fos = new FileOutputStream(file); while ( (readCount != -1)) { readCount = svins.readLine(readByte, 0, readByte.length); line = new String(readByte); line = line.trim(); if (line.startsWith(division)) break; fos.write(readByte, 0, readCount); } //while //保存并关闭 fos.flush(); fos.close(); file = null; }//try catch(Exception e){show("保存文件"+filename+"失败");return;} // continue; }else//if3------------------------------------------------------ {//不是文件 保存到hashtable show("不是文件 保存到hashtable"); //得到name值 index = line.indexOf("name="); name = line.substring(index+5); name = name.substring(1,name.length()-1); if (name.indexOf('"') !=-1){ index = name.indexOf('"'); name = name.substring(0,index); } show("name="+name+"---"); //下读一行 readCount = svins.readLine(readByte, 0, readByte.length); while((readCount != -1)){ readCount = svins.readLine(readByte, 0, readByte.length); line = new String(readByte);line = line.trim(); if (line.startsWith(division)) break; }//while }//if3 }//if2 }//if1 show("=================================="); // readCount = svins.readLine(readByte, 0, 1024); // line = new String(readByte);line = line.trim(); }//while1 svins.close();}//doUpload2//-----------------目标路径----------------------------------public void setTargetPath(String path){ this.targetpath=path;}public String getTargetPath(String path){ return this.targetpath;}//------------------------close-----------------------------------private void close(){ try{ if (svins != null) svins.close(); if (fos != null) fos.close(); }catch(IOException ioe){} catch(Exception e){show("close()失败 ");return;}}////////////////////////////show//////////////////////////////////////////////////private void show(Object Message){ String err = String.valueOf(Message); System.out.println(this.me+"->show: "+err);}} jsp 图片上传 显示 问题 action跳转到jsp页面后的执行问题。 java EE 5.0与mail.jar有冲突,应该怎么替换mail.jar? Servlet action is currently unavailable的错误 [求助]struts上传文件问题? 一个servlet的问题 求救各位高手!急 高手救命 如何在servlet中的doPost的out.println中再执行doGet中的方法 奇怪的数据库问题,我快被折磨疯了,谁来救救我???? java如何循环解析报文里面节点值 询求解密帮助。 JSP
package sm.net.upload;import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;public class upload {
String me = "sm.net.upload.upload";
int filecount = 0; //总共上传文件数量
int currentNo = 0; //当前上传文件编号
String division = ""; //分割字符 例如:----------7d32bf24409de File file;
FileOutputStream fos;
ServletInputStream svins; byte[] readByte = new byte[4096]; //输入流缓冲区
int readCount; //readCount 记录从输入流中实际读取的字符数
String line; //行
String name,filename, targetpath; //上传名称,文件名称,目标路径,
int key, len, index; //返回值,字符长度,index位置
long cx = 0; //循环器 Hashtable ht = null;//上传的其他非文件值//----------------------------------------------------------------------------------
public upload() {
targetpath = "";
readCount = 0;
ht = new Hashtable();
}//---------------------doUpload------------------------------------
public void doUpload(HttpServletRequest req) throws ServletException,IOException
{
String fileinfo,No;//文件信息,编号
boolean isFile;
show("开始上传");
try{
svins = req.getInputStream();
}catch(Exception e){
show("初始化servletInputStream失败");
svins.close(); return; }
//读取第一行
readCount = svins.readLine(readByte, 0, readByte.length);
line = new String(readByte);line = line.trim();
//得到分割符
division = line;
show("分割符 division='"+division+"'");
//开始循环读取数据
while(readCount != -1){//while1
//一个节点的分析与写入
if (line.startsWith(division) ){//if1
cx =cx +1;No = String.valueOf(cx);
show("----发现节点分割符---"+No);
//下读一行
readCount = svins.readLine(readByte, 0, readByte.length);
line = new String(readByte);line = line.trim();
//确定是否为文件
if (line.startsWith("content-")&&(line.indexOf("name=")!=-1)){//if2
show("开始分析文件头"+No);
if (line.indexOf("filename=")!=-1){//if3
//是文件 得到文件名称
isFile = true;
index = line.lastIndexOf("\\");
filename = line.substring(index+1);
filename = filename.substring(0,filename.length()-1);
if (filename.indexOf('"') !=-1){
index = filename.indexOf('"');
filename = filename.substring(0,index);
}
show("filename:"+filename+No);
//下读二行-------------
readCount = svins.readLine(readByte, 0, readByte.length);
readCount = svins.readLine(readByte, 0, readByte.length);
//写文件---------------
try{
file = new File(filename);
fos = new FileOutputStream(file);
while ( (readCount != -1)) {
readCount = svins.readLine(readByte, 0, readByte.length);
line = new String(readByte);
line = line.trim();
if (line.startsWith(division))
break;
fos.write(readByte, 0, readCount);
} //while
//保存并关闭
fos.flush();
fos.close();
file = null;
}//try
catch(Exception e){show("保存文件"+filename+"失败");return;}
// continue;
}else//if3------------------------------------------------------
{//不是文件 保存到hashtable
show("不是文件 保存到hashtable");
//得到name值
index = line.indexOf("name=");
name = line.substring(index+5);
name = name.substring(1,name.length()-1);
if (name.indexOf('"') !=-1){
index = name.indexOf('"');
name = name.substring(0,index);
}
show("name="+name+"---");
//下读一行
readCount = svins.readLine(readByte, 0, readByte.length);
while((readCount != -1)){
readCount = svins.readLine(readByte, 0, readByte.length);
line = new String(readByte);line = line.trim();
if (line.startsWith(division)) break;
}//while
}//if3
}//if2
}//if1
show("==================================");
// readCount = svins.readLine(readByte, 0, 1024);
// line = new String(readByte);line = line.trim();
}//while1
svins.close();
}//doUpload2
//-----------------目标路径----------------------------------
public void setTargetPath(String path){
this.targetpath=path;
}
public String getTargetPath(String path){
return this.targetpath;
}//------------------------close-----------------------------------
private void close(){
try{
if (svins != null) svins.close();
if (fos != null) fos.close();
}catch(IOException ioe){}
catch(Exception e){show("close()失败 ");return;}
}
////////////////////////////show//////////////////////////////////////////////////
private void show(Object Message){
String err = String.valueOf(Message);
System.out.println(this.me+"->show: "+err);
}
}