smartupload会出现这个问题;但常常用common-upload apache的那个组件;
smartupload有个函数可以设置上传的上限;
smartupload是写于内存中;common-upload 是写入临时文件中;
smartupload有个函数可以设置上传的上限;
smartupload是写于内存中;common-upload 是写入临时文件中;
解决方案 »
- 新手求助!
- 使用Connection的问题?
- 最近在学习SOA,想问一下:webservice和消息队列是什么关系啊?
- 关于j2ee中properties文件的问题
- 请教经常用JAVA相关大软件朋友关于硬件配置的问题。谢谢。
- 怎么读不到Properties文件属性的值呢
- Struts问题: 在 Action的execute()里面, 为什么mapping.getInputForward() == null ?
- 请教一个简单的EJB-QL的语法问题,请多多指教
- 高分求 jbuilder 6.0 的SN
- Ubuntu系统下运行Eclipse出现遭遇找不到jre的问题
- 请问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/
问题总算解决了,谢谢大家。