smartupload 问题

wanghao1987 2009-04-26 08:20:17
这是抛出的异常:java.lang.IllegalArgumentException: Files' name is invalid or does not exist (1205).

下面是页面代码:

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>传送文件</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" id="Input_file"><br>
<input type="submit" value="上传" />
</form>
</body>
</html>


下面是后台代码:

package com.wanghao.upload;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;
public class Upload extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response){
try {
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(), request, response);
su.upload();
com.jspsmart.upload.File f=su.getFiles().getFile(0);//异常在这一行抛出
String fname = f.getFileName(); // 文件名
f.saveAs("/updfile/"+fname, f.SAVEAS_VIRTUAL);
} catch (ServletException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmartUploadException e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request,response);
}

}
jar包已正常导入,web.xml也配置正确!不知道错误出在哪里?请高手指教
...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mike_24 2009-04-26
  • 打赏
  • 举报
回复
UP
wzju64676266 2009-04-26
  • 打赏
  • 举报
回复
在jsp页面上处理上传要用smart.initialize(pageContext) ;这个跟servlet有点不同



<%@ page contentType="text/html; charset=GBK"%>
<jsp:useBean id="smart" scope="page" class="org.lxh.smart.SmartUpload"/>
<%
// 1、上传初始化
request.setCharacterEncoding("GBK");
smart.initialize(pageContext) ;
smart.setMaxFileSize(1000000111);
smart.setTotalMaxFileSize(900000001);
// smart.setAllowedFilesList("jpg,txt,doc,exe,gif,bmp,mp3");
smart.setDeniedFilesList("jsp,html,php,aspx,asp");
// 2、准备上传
smart.upload() ;
// 3、保存上传的文件
// smart.save("/upload") ;
int count=smart.getFiles().getCount();
out.println("<font>"+count+"</font>个文件上传成功<br>");
for(int i=0;i<count;i++)
{
//这里要输入org.lxh.smart.因为这里要指明是哪里的File
org.lxh.smart.File file=smart.getFiles().getFile(i);

String name = smart.getRequest().getParameter("name"+i) ;
String ext = smart.getFiles().getFile(i).getFileExt() ;
file.saveAs("/upload/"+name+"."+ext) ;
out.println("<center>");
out.println("<table border=1>");
out.println("<tr><td>表单项名"+file.getFieldName()+"</td></tr>");
out.println("<tr><td>文件长度"+file.getSize()+"</td></tr>");
out.println("<tr><td>文件扩展名"+file.getFileExt()+"</td></tr>");
out.println("<tr><td>文件全名"+file.getFilePathName()+"</td></tr>");
out.println("<tr><td><img src=./upload/"+name+"."+ext+" height=250 width=200></tr></td>");
out.println("</table>");
out.println("</center>");


}
//不能直接用request
%>
wzju64676266 2009-04-26
  • 打赏
  • 举报
回复
这是我做的一个传多张照片的,在servlet里要加上
ServletConfig config;
public void init(ServletConfig config) throws ServletException
{
this.config=config;
}

smart.initialize(config, request, response);//初始化







源代码
package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import Bean.*;
import Entity.*;
import org.lxh.smart.SmartUpload;
import org.lxh.smart.*;
public class ImageDealServlet extends HttpServlet {
public ImageDealServlet() {

}

private static final String CONTENT_TYPE = "text/html; charset=GBK";

ServletConfig config;
public void init(ServletConfig config) throws ServletException
{
this.config=config;
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
try
{
String flag=request.getParameter("flag");
if(flag.equals("addImage"))
{

response.setContentType(CONTENT_TYPE);
SmartUpload smart = new SmartUpload();
// 1、上传初始化
smart.initialize(config, request, response);
smart.setMaxFileSize(1000000111);
smart.setTotalMaxFileSize(900000001);
// smart.setAllowedFilesList("jpg,txt,doc,exe,gif,bmp,mp3");
smart.setDeniedFilesList("jsp,html,php,aspx,asp");
// 2、准备上传
smart.upload();
// 3、保存上传的文件
// smart.save("/upload") ;
int count = smart.getFiles().getCount();
System.out.println("上传" + count + "个文件上传");
boolean suc = false;
for (int i = 1; i <=count; i++)
{
//这里要输入org.lxh.smart.因为这里要指明是哪里的File
org.lxh.smart.File file = smart.getFiles().getFile(i-1);
String description =smart.getRequest().getParameter("description"+i);
String fileName = smart.getFiles().getFile(i-1).getFileName();
String ext = smart.getFiles().getFile(i-1).getFileExt();
file.saveAs("/upload1/" + fileName+i+ "." + ext);
ImageDAO imageDao = new ImageDAO();
if(imageDao.insertImage("./upload1/" + fileName+i+ "." + ext,description))
{
suc=true;
}
else
{
suc=false;
break;
}

}
if(suc)
{
System.out.println("文件上传成功");
}
else
{
System.out.println("文件上传失败");
}
}//end if flag.equals("addImage")
else if(flag.equals("getImage"))
{
ImageDAO imageDao = new ImageDAO();
String idd=request.getParameter("id");
ArrayList array=null;
int pageCount=imageDao.getPageCount();
if(idd==null)
{
array=imageDao.getImgageInfo();
}
else
{
int id=Integer.parseInt(request.getParameter("id"));
if(id==0)
{
array=imageDao.getImgageInfo();
}
else
{
array=imageDao.getImageInfoById(id);
}

}

request.setAttribute("imageInfo",array);
request.setAttribute("get","true");
request.getRequestDispatcher("scanimage.jsp").forward(request,response);
}

}
catch (Exception ex)
{
ex.printStackTrace();
}


}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//Clean up resources
public void destroy() {
}

private void jbInit() throws Exception {
}
}
  • 打赏
  • 举报
回复

<%@ page import="java.io.*" %>
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>上传</title>
</head>
<body bgcolor="ffffff">

<%
request.setCharacterEncoding("gb2312");
DataInputStream din = null;
FileOutputStream fileOut = null;
String contentType = request.getContentType();
out.println(contentType + "<br>");
din = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
out.println("File Length:" + formDataLength + "<br>");

byte dataBytes[] = new byte [formDataLength];
try{
din.readFully(dataBytes);
}
catch(Exception e){}
try{
fileOut = new FileOutputStream("c:\\temp.txt");
fileOut.write(dataBytes);
fileOut.close();
String fileContent = new String(dataBytes);
out.println(fileContent);
}
catch(Exception fe){}


%>
</body>

</html>
  • 打赏
  • 举报
回复

package com.bjsxt.shopping.util.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;

public class FileUpload extends HttpServlet {
//BLOB

@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}

private ServletConfig config = null;



private File tempPath = new File("D:\\upload\\temp\\"); // 用于存放临时文件的目录

public void destroy() {
config = null;
super.destroy();
}


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int id = -1;

String uploadPath = config.getInitParameter("uploadPath"); // 用于存放上传文件的目录

res.setContentType("text/html; charset=GB2312");
PrintWriter out = res.getWriter();
System.out.println(req.getContentLength());
System.out.println(req.getContentType());
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(tempPath);

ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000);
try {
List fileItems = upload.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator iter = fileItems.iterator();

// 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$";

// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".jsp" };
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if(item.isFormField()) {
if(item.getFieldName().equals("id")) {
id = Integer.parseInt(item.getString());
}
}
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
}
try {

// 保存上传的文件到指定的目录

// 在下文中上传文件至数据库时,将对这里改写
item.write(new File(uploadPath + id + ".jpg"));

out.print(name + "  " + size + "<br>");
} catch (Exception e) {
out.println(e);
}

} else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
out.println(e);
} catch (FileUploadException e) {
out.println(e);
}

}



}

guolimin1118 2009-04-26
  • 打赏
  • 举报
回复
直接转到另外一个页面不就行了
wanghao1987 2009-04-26
  • 打赏
  • 举报
回复
当文件上传成功的时候,会弹出一个空白的网页,有没有办法禁止该网页的出现!
fanyuanwaifdl 2009-04-26
  • 打赏
  • 举报
回复
gz

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧