我在实现文件上传功能的时候,总是不能够获得完整的上传数据,在网络中流的available()方法是无效的,所以我从http头的content-length属性中获得上传长度,但是当上传长度超过1M的时候获得的上传数据又不全了,请问有没有好的办法获得完整的上传数据?
解决方案 »
- 〓〓运行一段时间后new URL抓取网页的servlet就停止工作了?〓〓
- dwr中配置的convert 没有起作用,麻烦大家帮忙看一下什么问题
- 两次提交后,WEB程序不能正常执行! (大虾帮忙啊!)
- 关于JSPSMARTUPLOAD
- web.xml 分布式处理
- 那位大哥,能帮我一下
- Servlet中可不可以使用out.println()方法?
- javabean <jsp:setProperty name="demand" property="*" /> 方式出现的问题, bean里面有一个Date属性,表单传递过去的是字符串,出错,
- 求教备份方案!
- spring中业务逻辑层的接口和实现类的在Action中的注入问题
- java2d 绘制简单曲线图。谁做过,帮帮忙。
- 求一个正则表达试
HttpServletRequest request, HttpServletResponse response) throws Exception {
GoodsForm goodsForm = (GoodsForm) form;// TODO Auto-generated method stub
FormFile picFile = goodsForm.getPicFile();
//得到唯一的文件名
String fn = "uploadfiles/" + this.getUniqueName(picFile.getFileName());
//得到物理路径
ServletContext application = request.getSession().getServletContext();
String realPath = application.getRealPath("/" + fn);
System.out.println(realPath);
//保存
FileOutputStream os = new FileOutputStream(realPath);
os.write(picFile.getFileData());
os.close();
//设置实体类的相关属性
goodsForm.getGoods().setPic(fn);
System.out.println("保存至数据库的路径:"+fn);
//调用业务类对Goods类进行处理
response.getWriter().println("<img src='"+fn+"' >");
return null;
}
String getUniqueName(String oldName)
{
int dotIndex = oldName.lastIndexOf(".");
String extName = "";
if( dotIndex >= 0 )
extName = oldName.substring(dotIndex);
return df.format(new Date()) + extName;
}
static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
}
但愿能帮上你
我用的是这种,package com.upload;import com.jspsmart.upload.SmartUpload;
/*****
*
*author:East(张栋芳)
*date: 2008-7-19
*note: 上传文件和下载文件
*/public class Upload {
/***
*
*上传文件平
*/
public void upLoad(){
// 新建一个SmartUpload对象
SmartUpload supload = new SmartUpload();
// 上传初始化
supload.initialize(pageContext);
// 限制每个上传文件的最大长度
supload.setMaxFileSize(10000);
// 限制总上传数据的长度。
supload.setTotalMaxFileSize(20000);
// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
supload.setAllowedFilesList("doc,txt");
// 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
supload.setDeniedFilesList("exe,bat,jsp,html,htm");
// 上传文件
supload.upload();
// 将上传文件全部保存到指定目录
int count = supload.save("/upload");
com.jspsmart.upload.SmartFile file = supload.getFiles().getFile(0);
//文件名
String fileName = file.getFieldName();
}
/***
*
*下载文件
*/
public void downLoad(){
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开 su.setContentDisposition(null);
su.downloadFile("/upload/test.doc");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String dir = request.getSession().getServletContext().getRealPath("/")
+ File.separator + "uploadDir";
String fileName="";
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(10485760); // 10M
// fu.setRepositoryPath( "c:/b" );
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
System.out.println("得到请求内容的长度:" + request.getContentLength());
try {
List fileItems = fu.parseRequest(request);
if (fileItems.size() > 0) {
System.out.println("上传文件数目:" + fileItems.size());
}
Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next();
fileName = new File(item.getName()).getName(); File destDir = new File(dir);
if (!destDir.exists()) {
destDir.mkdirs();
}
File file = new File(destDir, fileName);
item.write(file);
} Workbook workbook = Workbook.getWorkbook(new File(dir+File.separator+fileName));
Sheet sheet = workbook.getSheet(0);
Cell cell = null; int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) { cell=sheet.getCell(j, i);
System.out.print(cell.getContents().trim());
System.out.print("\t");
}
System.out.print("\n");
}
// 关闭它,否则会有内存泄露
workbook.close(); } catch (FileUploadException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}finally
{
File deleteFile=new File(dir+File.separator+fileName);
deleteFile.delete();
}
}
这篇文章详细地分析了上传组件COS及与其他上传组件smartUpload、FileUpload的性能对比 希望对你有帮助
http://blog.csdn.net/hingwu/archive/2007/05/14/1608624.aspx
我的上传要求是不使用jdk与j2ee以外的第三方包,而是自己分析上传流。现在是要准确的获得上传流的数据长度,虽然现在有一个蠢办法,但是我觉得应该还有更好的其它办法实现的,因为我觉得我现在使用的办法在性能上存在很大问题,也可能会造成较大漏洞。