求助,JSP文件上传问题!!! smartupload会出现这个问题;但常常用common-upload apache的那个组件;smartupload有个函数可以设置上传的上限;smartupload是写于内存中;common-upload 是写入临时文件中; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我试了一下common-upload,可是还是有错误,E文太次了,apache组件不会用。我用JSP调试的,我只要求一次上传一个100K以下的图片,并可显示图片地址。我着急用,高手帮一下忙吧。请求页面up.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><form action="up1.jsp" method="post" enctype="multipart/form-data" name="form1"> <input name="thisfile" type="file" id="thisfile"> <input name="user" type="text" id="user" size="10"> <input type="submit" name="Submit" value="Submit"></form></body></html>处理页面 up1.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><%@ page import="org.apache.commons.fileupload.*" %><%@ page import="java.util.*" %><%@ page import="java.io.*" %><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><% try{ DiskFileUpload fu = new DiskFileUpload(); // 设置最大文件尺寸,这里是4MB fu.setSizeMax(4194304); // 设置缓冲区大小,这里是4kb fu.setSizeThreshold(4096); // 设置临时目录: fu.setRepositoryPath("e:\\test\\"); // 得到所有的文件: List fileItems = fu.parseRequest(request); Iterator i = fileItems.iterator(); // 依次处理每一个文件: while(i.hasNext()) { FileItem fi = (FileItem)i.next(); // 获得文件名,这个文件名包括路径: String fileName = fi.getName(); // 在这里可以记录用户和文件信息 // ... // 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名: out.print("写入成功"); fi.write(new File( "e:\\test\\" + "a.txt")); } } catch(Exception e) { out.print("写入error");} %></body></html>执行结果是:写入error 写个servlet来处理吧引入的包: import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;部分代码:SmartUpload mySmartUpload = new SmartUpload();//初始化mySmartUpload.initialize(config, request, response);//设置上传文件类型的限制为xml文件mySmartUpload.setAllowedFilesList("jpg"); try { mySmartUpload.upload();} catch (ServletException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();} catch (SmartUploadException e) { e.printStackTrace();}config是在servlet中定义private ServletConfig config;在init(ServletConfig config)中this.config = config;试试 上面错了。。“设置上传文件类型的限制为xml文件”应该为“设置上传文件类型的限制为jpg文件”,可以添加其他类型。可以用setMaxFileSize()和setTotalMaxFileSize()来设置文件大小。 谢谢!!还是出现内存溢出错误。servlet不经常用,可不可以给个JAVABEAN+JSP的例子。 利用 commons-fileupload.jar 上传: 下载地址:http://jakarta.apache.org/site/downloads/downloads_commons-fileupload.cgi文件指定页面:<%@ page contentType="text/html;charset=gbk"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Big5"> <title>inputUpload</title> </head> <body> <% request.setCharacterEncoding("gbk"); %> <form action="getupload.jsp" enctype="multipart/form-data" method="POST" > 要上?的文件:<input type="FILE" name="file"/> <input type="submit" value="上?"/> </form> </body></html>下面是上传的后台文件 记着下载 apache commons中的fileupload包 <%@ page contentType="text/html;charset=GBK"%><%@ page import="java.util.*"%><%@ page import="java.io.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.beanutils.*"%> <% DiskFileUpload dfu = new DiskFileUpload(); // 设置允许用户上传文件大小,单位:字节 dfu.setSizeMax(1000000); // maximum size that will be stored in memory? // 设置最多只允许在内存中存储的数据,单位:字节 dfu.setSizeThreshold(4096); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录 dfu.setRepositoryPath("f:\\public"); //开始读取上传信息 try{ List fileItems = dfu.parseRequest(request); %> <% // 依次处理每个上传的文件 Iterator i = fileItems.iterator(); String name =null; long size=0; while (i.hasNext()) { FileItem fi = (FileItem) i.next(); //忽略其他不是文件域的所有表单信息 if (!fi.isFormField()) { name = fi.getName(); size = fi.getSize(); if((name==null||name.equals("")) && size==0) continue; } name=fi.getName(); size=fi.getSize(); name = name.replace(':','_'); name = name.replace('\\','_'); File writeFile=new File("F:\\public",name); fi.write(writeFile); } }catch(FileUploadException fue) { fue.printStackTrace();} %> commons-beanutils.jar这个包也要的. 谢谢,和我上面的例子一样还是抛出异常。如:http://free4.e-168.cn/goodok/jsp1/fileupload/upload.html我给大家一个FTP大家帮我在FTP里改吧请进入我的FTP的JSP1文件夹,里面有5个文件夹,请帮忙的朋友看里面的说明,我把问题都写到说明里了。请大家一定帮我改成功,再次感谢。ftp4.e-168.com用户名:goodok密码: 123456789访问地址根目录:http://free4.e-168.cn/goodok/ 谢谢,和我上面的例子一样还是抛出异常。如:http://free4.e-168.cn/goodok/jsp1/fileupload/upload.html我给大家一个FTP大家帮我在FTP里改吧请进入我的FTP的JSP1文件夹,里面有5个文件夹,请帮忙的朋友看里面的说明,我把问题都写到说明里了。请大家一定帮我改成功,再次感谢。ftp4.e-168.com用户名:goodok密码: 123456789访问地址根目录:http://free4.e-168.cn/goodok/ 问题解决了,原来是我自己和自己过不去,以前习惯用TT的在新窗口中打开网页,结果害了自己好几天。不是上传的文件0KB就是内存溢出,浪费了不少时间。我改成在当前窗口提交网页就好了。问题总算解决了,谢谢大家。 jquery 显示,隐藏元素方法 response.sendRedirect和request.getRequestDispatcher().forward什么时候用哪个? hibernate lazy 延迟加载问题 struts2的action标签怎么跳转到制定的action JXL的封装问题 谁可以挽救我的系统,要多少分就给多少,高手快来啊! Timer 问题 请问在JAVA中如何调用DLL,最好能有个详细点的例子。 怎么启动我的JVM?? 不用注册,到哪里DOWN weblogic? 请问JAVA中画点函数是什么? 除错,xml数据显示
我用JSP调试的,我只要求一次上传一个100K以下的图片,并可显示图片地址。
我着急用,高手帮一下忙吧。
请求页面up.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<form action="up1.jsp" method="post" enctype="multipart/form-data" name="form1">
<input name="thisfile" type="file" id="thisfile">
<input name="user" type="text" id="user" size="10">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>处理页面 up1.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
try{
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath("e:\\test\\"); // 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while(i.hasNext()) {
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
// 在这里可以记录用户和文件信息
// ...
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
out.print("写入成功");
fi.write(new File( "e:\\test\\" + "a.txt"));
}
}
catch(Exception e)
{
out.print("写入error");
} %></body>
</html>
执行结果是:写入error
引入的包:
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
部分代码:
SmartUpload mySmartUpload = new SmartUpload();
//初始化
mySmartUpload.initialize(config, request, response);
//设置上传文件类型的限制为xml文件
mySmartUpload.setAllowedFilesList("jpg");
try {
mySmartUpload.upload();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmartUploadException e) {
e.printStackTrace();
}
config是在servlet中定义private ServletConfig config;在init(ServletConfig config)中this.config = config;
试试
可以用setMaxFileSize()和setTotalMaxFileSize()来设置文件大小。
还是出现内存溢出错误。
servlet不经常用,可不可以给个JAVABEAN+JSP的例子。
文件指定页面:<%@ page contentType="text/html;charset=gbk"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Big5">
<title>inputUpload</title>
</head>
<body>
<%
request.setCharacterEncoding("gbk");
%>
<form action="getupload.jsp" enctype="multipart/form-data" method="POST" >
要上?的文件:<input type="FILE" name="file"/>
<input type="submit" value="上?"/>
</form>
</body>
</html>下面是上传的后台文件 记着下载 apache commons中的fileupload包
<%@ page contentType="text/html;charset=GBK"%><%@ page import="java.util.*"%><%@ page import="java.io.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.beanutils.*"%>
<% DiskFileUpload dfu = new DiskFileUpload(); // 设置允许用户上传文件大小,单位:字节 dfu.setSizeMax(1000000); // maximum size that will be stored in memory? // 设置最多只允许在内存中存储的数据,单位:字节 dfu.setSizeThreshold(4096); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录 dfu.setRepositoryPath("f:\\public"); //开始读取上传信息 try{ List fileItems = dfu.parseRequest(request);
%> <% // 依次处理每个上传的文件 Iterator i = fileItems.iterator(); String name =null; long size=0; while (i.hasNext()) { FileItem fi = (FileItem) i.next(); //忽略其他不是文件域的所有表单信息 if (!fi.isFormField()) { name = fi.getName(); size = fi.getSize(); if((name==null||name.equals("")) && size==0) continue; } name=fi.getName(); size=fi.getSize(); name = name.replace(':','_'); name = name.replace('\\','_'); File writeFile=new File("F:\\public",name); fi.write(writeFile); } }catch(FileUploadException fue) { fue.printStackTrace();} %>
如:http://free4.e-168.cn/goodok/jsp1/fileupload/upload.html
我给大家一个FTP大家帮我在FTP里改吧
请进入我的FTP的JSP1文件夹,里面有5个文件夹,请帮忙的朋友看里面的说明,我把问题都写到说明里了。请大家一定帮我改成功,再次感谢。
ftp4.e-168.com
用户名:goodok
密码: 123456789
访问地址根目录:
http://free4.e-168.cn/goodok/
如:http://free4.e-168.cn/goodok/jsp1/fileupload/upload.html
我给大家一个FTP大家帮我在FTP里改吧
请进入我的FTP的JSP1文件夹,里面有5个文件夹,请帮忙的朋友看里面的说明,我把问题都写到说明里了。请大家一定帮我改成功,再次感谢。
ftp4.e-168.com
用户名:goodok
密码: 123456789
访问地址根目录:
http://free4.e-168.cn/goodok/
问题总算解决了,谢谢大家。