这是我的Action 不知道为什么,上传到目录的时候,都是一个叫Content-Disposition的空白文件,0KB,不知道为什么
package uppower.action;import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流
//作者:jjcc 2007.10.26
/*Connection con=DriverManager.getConnection("org.gjt.mm.mysql.Driver","gonglin","gonglin");
Statement St_xh=con.createStatement();
//设置上传服务器路径
String mypath_m =application.getRealPath("D:\\temp\\");
if((System.getProperty("os.name").toLowerCase().indexOf("sun"))>-1)
{
mypath_m+="/docup/";
}
else
{
mypath_m+="\\docup\\";
}
*/
String[][] arr_param = new String[20][2]; //可以存放20个参数的数组
int param_len=0; //参数数组的长度
String message= ""; //出错信息
String folder = ""; //文件夹参数
char[] hchl={13,10}; //char(10)表示回车 char(13)表示换行
String boundary=request.getContentType().substring(30);
String field_boundary="--"+boundary+new String(hchl);
String last_boundary="--"+boundary+"--"+new String(hchl);
System.out.println("boundary: "+boundary);
//System.out.println("field_boundary: "+field_boundary);
//System.out.println("hchl: "+new String(hchl));
ServletInputStream getdata=request.getInputStream(); //客户端的输入流
ByteArrayOutputStream temp=new ByteArrayOutputStream(); //建立临时2进制输出流
byte[] data_line=new byte[8192]; //临时读取字符数组
int line_byte_count=0; //一次读入的字节数
boolean found_boundary=false; //边界标志
while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){ //读入第一个文件分割符
if(!found_boundary){
line_byte_count=getdata.readLine(data_line,0,data_line.length);
}
String temp_str=new String(data_line,0,line_byte_count); //将读入的字节形成字符串
//System.out.println("temp_str: "+temp_str);
if(temp_str.indexOf("filename")!=-1){ //读入的是file数据
if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("\"")+1).length()> 2){
String file_name=new String(temp_str.substring(temp_str.lastIndexOf("\\")+1,temp_str.lastIndexOf("\"")).getBytes("ISO8859_1"),"EUC-CN");
/*//对文件名的处理
if(file_name.matches("^.*缓凝.+$")) {
file_name = "HNJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*减阻.+$")) {
file_name = "JZJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*降失水.+$")) {
file_name = "JSSJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*水泥质检.+$")) {
file_name = "SNZJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*取样.+$")) {
file_name = "QY.xls";
}
*/
line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type信息
line_byte_count=getdata.readLine(data_line,0,data_line.length); //文件信息第一行
//在服务器端生成文件
/*ResultSet Rst_xh=St_xh.executeQuery("select to_char(sysdate,'yyyymmddhh24miss') xh from dual");
Rst_xh.next();
String f_xh=Rst_xh.getString("xh");
Rst_xh.close();
folder += mypath_m +f_xh+file_name+"@";
*/
FileOutputStream myfile=new FileOutputStream("d:\\temp\\"+file_name,false);
boolean test=true; //在此文件中的标志
while(test) {
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){ //到文件流的末尾,退出
test=false;
break;
}
if(temp.size()==0){ //使用 空的 临时2进制输出流
temp.write(data_line,0,line_byte_count);
}else{
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
//如果是文件分隔符
myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2); //去掉最后的回车换行符
temp.reset(); //清空临时2进制输出流
myfile.close();
System.out.println(file_name+"上传成功了 <br> "); message += "上传成功了";
test=false;
found_boundary=true;
}else{
temp.writeTo(myfile);
temp.reset();
temp.write(data_line,0,line_byte_count);
}
}
}
}else{ //读入的不是文件
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
found_boundary=true;
message = field_name+"没有选择上传文件! \\n ";
}
} else{ //不是文件部分
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("\""));
line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type
temp.reset();
boolean test=true;
while(test) {
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){
test=false;
break;
}
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
test=false;
found_boundary=true;
if(temp.size()> 2){
arr_param[param_len][0]=field_name;
arr_param[param_len][1]=new String(temp.toByteArray()).substring(0,new String(temp.toByteArray()).length()-2);
param_len++;
}else{
arr_param[param_len][0]=field_name;
arr_param[param_len][1]="null";
param_len++;
}
temp.reset();
}else{
temp.write(data_line,0,line_byte_count); //temp里存放的是data_line上一次读入的数据
}
}
}
}
getdata.close();
for(int i=0;i < param_len;i++) {
System.out.println(arr_param[i][0]+" = "+arr_param[i][1]);
}
System.out.println("folder: "+folder);
return mapping.findForward("success");
}}
这段就是在网上找的,我自己改了一些,就不行了,不知道为什么 JSP文件里面是这样的<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*"%><%pageContext.setAttribute("jsp.tags.reuse", new Boolean(false));%>
<A:navigation/>
<hr>
<center><b><font size=+1><bean:message key="upload.name"/></font></b></center>
<html>
<head>
<title>????</title>
</head>
<body><form action="<%=request.getContextPath() %>/UploadAction.do" name="one" enctype="multipart/form-data" method="post"><p align="center">文件上传
<input type="File" name="fileupload" value="upload" />
<input type="submit" value="上传">
<input type="reset" value="取消">
</p>
</form>
</body>
</html>希望朋友们帮我看一下,我们正在做一个参加学校比赛的项目,谢谢了
package uppower.action;import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//jsp文件上传源码,单一或多文件也可与其他标单类型混杂使用,可不限制上传大小,速度一流
//作者:jjcc 2007.10.26
/*Connection con=DriverManager.getConnection("org.gjt.mm.mysql.Driver","gonglin","gonglin");
Statement St_xh=con.createStatement();
//设置上传服务器路径
String mypath_m =application.getRealPath("D:\\temp\\");
if((System.getProperty("os.name").toLowerCase().indexOf("sun"))>-1)
{
mypath_m+="/docup/";
}
else
{
mypath_m+="\\docup\\";
}
*/
String[][] arr_param = new String[20][2]; //可以存放20个参数的数组
int param_len=0; //参数数组的长度
String message= ""; //出错信息
String folder = ""; //文件夹参数
char[] hchl={13,10}; //char(10)表示回车 char(13)表示换行
String boundary=request.getContentType().substring(30);
String field_boundary="--"+boundary+new String(hchl);
String last_boundary="--"+boundary+"--"+new String(hchl);
System.out.println("boundary: "+boundary);
//System.out.println("field_boundary: "+field_boundary);
//System.out.println("hchl: "+new String(hchl));
ServletInputStream getdata=request.getInputStream(); //客户端的输入流
ByteArrayOutputStream temp=new ByteArrayOutputStream(); //建立临时2进制输出流
byte[] data_line=new byte[8192]; //临时读取字符数组
int line_byte_count=0; //一次读入的字节数
boolean found_boundary=false; //边界标志
while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){ //读入第一个文件分割符
if(!found_boundary){
line_byte_count=getdata.readLine(data_line,0,data_line.length);
}
String temp_str=new String(data_line,0,line_byte_count); //将读入的字节形成字符串
//System.out.println("temp_str: "+temp_str);
if(temp_str.indexOf("filename")!=-1){ //读入的是file数据
if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("\"")+1).length()> 2){
String file_name=new String(temp_str.substring(temp_str.lastIndexOf("\\")+1,temp_str.lastIndexOf("\"")).getBytes("ISO8859_1"),"EUC-CN");
/*//对文件名的处理
if(file_name.matches("^.*缓凝.+$")) {
file_name = "HNJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*减阻.+$")) {
file_name = "JZJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*降失水.+$")) {
file_name = "JSSJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*水泥质检.+$")) {
file_name = "SNZJ.xls";
System.out.println("file_name: "+file_name);
}
else if(file_name.matches("^.*取样.+$")) {
file_name = "QY.xls";
}
*/
line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type信息
line_byte_count=getdata.readLine(data_line,0,data_line.length); //文件信息第一行
//在服务器端生成文件
/*ResultSet Rst_xh=St_xh.executeQuery("select to_char(sysdate,'yyyymmddhh24miss') xh from dual");
Rst_xh.next();
String f_xh=Rst_xh.getString("xh");
Rst_xh.close();
folder += mypath_m +f_xh+file_name+"@";
*/
FileOutputStream myfile=new FileOutputStream("d:\\temp\\"+file_name,false);
boolean test=true; //在此文件中的标志
while(test) {
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){ //到文件流的末尾,退出
test=false;
break;
}
if(temp.size()==0){ //使用 空的 临时2进制输出流
temp.write(data_line,0,line_byte_count);
}else{
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
//如果是文件分隔符
myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2); //去掉最后的回车换行符
temp.reset(); //清空临时2进制输出流
myfile.close();
System.out.println(file_name+"上传成功了 <br> "); message += "上传成功了";
test=false;
found_boundary=true;
}else{
temp.writeTo(myfile);
temp.reset();
temp.write(data_line,0,line_byte_count);
}
}
}
}else{ //读入的不是文件
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
line_byte_count=getdata.readLine(data_line,0,data_line.length);
found_boundary=true;
message = field_name+"没有选择上传文件! \\n ";
}
} else{ //不是文件部分
String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("\""));
line_byte_count=getdata.readLine(data_line,0,data_line.length); //跳过Content-Type
temp.reset();
boolean test=true;
while(test) {
line_byte_count=getdata.readLine(data_line,0,data_line.length);
if(line_byte_count==-1){
test=false;
break;
}
if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
test=false;
found_boundary=true;
if(temp.size()> 2){
arr_param[param_len][0]=field_name;
arr_param[param_len][1]=new String(temp.toByteArray()).substring(0,new String(temp.toByteArray()).length()-2);
param_len++;
}else{
arr_param[param_len][0]=field_name;
arr_param[param_len][1]="null";
param_len++;
}
temp.reset();
}else{
temp.write(data_line,0,line_byte_count); //temp里存放的是data_line上一次读入的数据
}
}
}
}
getdata.close();
for(int i=0;i < param_len;i++) {
System.out.println(arr_param[i][0]+" = "+arr_param[i][1]);
}
System.out.println("folder: "+folder);
return mapping.findForward("success");
}}
这段就是在网上找的,我自己改了一些,就不行了,不知道为什么 JSP文件里面是这样的<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*"%><%pageContext.setAttribute("jsp.tags.reuse", new Boolean(false));%>
<A:navigation/>
<hr>
<center><b><font size=+1><bean:message key="upload.name"/></font></b></center>
<html>
<head>
<title>????</title>
</head>
<body><form action="<%=request.getContextPath() %>/UploadAction.do" name="one" enctype="multipart/form-data" method="post"><p align="center">文件上传
<input type="File" name="fileupload" value="upload" />
<input type="submit" value="上传">
<input type="reset" value="取消">
</p>
</form>
</body>
</html>希望朋友们帮我看一下,我们正在做一个参加学校比赛的项目,谢谢了
解决方案 »
- jsp找不到文件
- 怎样解决浏览器不兼容的问题。急!急!急!
- 我是学生!在公司进行web开发啊
- jsp文件下载问题!!!
- 写了一个连接池的类,却出现了一个莫名其妙的错误:Source not found for StandardWrapper.loadServlet() line: 1066
- 请教一个jsp中文得问题
- form 提交servlet问题,请指教。
- 大难题.从数据库查询数据显示在网页上造成IE出错.高手帮帮我!!!!!!!!
- 急需要j2sdk1.4.1_02的参考文件 ,各位帮个忙,多谢!!
- 哪位大虾可以给小弟介绍下session的用法?急!
- 求助!Linux下运行的Java程序怎么不能删除文件啊 ?
- 关于ireport做出来的格式,显示在html,然后打印的问题
用到的组件可以在apache上下载http://commons.apache.org/fileupload/
具体在我的帖子里
http://topic.csdn.net/u/20091111/17/8fa87372-b003-4ebb-90fe-db3b20c08311.htmlpackage com;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;/**
* Servlet implementation class for Servlet: UploadServlet
*
*/
public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
File tmpDir = null;//初始化上传文件的临时存放目录
File saveDir = null;//初始化上传文件后的保存目录
String msg="";
int l=0;
public UploadServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
if(ServletFileUpload.isMultipartContent(request)){
String fileName="";
DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
dff.setRepository(tmpDir);//指定上传文件的临时目录
dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
//sfu.setFileSizeMax(5*1024*1024);//指定单个上传文件的最大尺寸
sfu.setSizeMax(5*1024*1024);//指定一次上传多个文件的总尺寸
List items=sfu.parseRequest(request);
Iterator fii = items.iterator();//解析request 请求,并返回FileItemIterator集合
while(fii.hasNext()){
FileItem fis = (FileItem)fii.next();//从集合中获得一个FileItem
if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
int i=fis.getName().lastIndexOf("\\");
if(i<0){
fileName=fis.getName();//兼容非ie
}
else{
fileName = fis.getName().substring(i);//获得上传文件的文件名
}
System.out.println("取得文件请求"+fis.getFieldName()+"他的名是:"+fis.getName()+"值是"+fis.getString());
BufferedInputStream in = new BufferedInputStream(fis.getInputStream());//获得文件输入流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"\\"+fileName)));//获得文件输出流
msg=msg+"文件"+fileName+"上传成功\\n";
Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹
in.close();
out.flush();
out.close();
}
else if(fis.isFormField()){
System.out.println("取得非文件请求"+fis.getFieldName()+"他的名是:"+fis.getName()+"值是"+fis.getString());
}
}
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
java.io.PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<script>window.parent.Finish('"+msg+"');</script>");//终于成功了,还不到你的上传文件中看看,你要的东西都到齐了吗
out.println("</body>");
out.println("</html>");
msg="";
}
}catch(Exception e){
e.printStackTrace();
}
} public void init() throws ServletException {
/* 对上传文件夹和临时文件夹进行初始化
*
*/
super.init();
String tmpPath = "C:\\WINDOWS\\temp";
String savePath = "E:\\Test\\upload";
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if(!tmpDir.isDirectory())
tmpDir.mkdir();
if(!saveDir.isDirectory())
saveDir.mkdir();
}
}
5M~(当然可以任意设置限制)
那种方式我试过,文件太大会内存溢出!
JspSmartUpload组件一般 COS组件效果比较好~