请问怎样实现用JSP上传图片到文件夹里!!希望各位能够提供源代码给我,在下感激不尽!!多谢了
解决方案 »
- java.lang.NullPointerException
- 《零基础学java web开发》pdf版 作者:刘聪 ,不要iso和ppt要书 有的发邮箱[email protected]
- 一道面试题....
- 求救 ,hibernate 如何禁止缓存
- session 老是提示出错
- 开发邮件系统的疑问?急!!
- Java中文乱码问题,我将邮件内容直接导入到数据库,全部变成了乱码,然后我想在页面上显示出来?该怎么处理?
- 急 在jsp中怎样从数据库中读取图片
- 一个页面中,一个分帧提取另一分帧中数据的问题?
- Tomcat配置SSL时出现的问题
- 通过JSP上传图片,如何在上传之前预览图片?
- 用JSP生成树形目录
到google上查查
D:\java\tomcat\work\Standalone\localhost\_\jspsmartupload\jsp\sample1$jsp.java:3: Package com.jspsmart.upload not found in import.
import com.jspsmart.upload.*;
^
1 error at org.apache.jasper.compiler.Compiler.compile(Compiler.java:285)
at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:548)
http协议,分析request的getInputStream(),把随机界限中的图片
二进制信息提取出来,分析表单,然后输入到文件或者数据库就OK了.
当然。你的路径和我的不同
另外。你的jspsmartupload下的web-inf\classes下的东西都copy到虚拟目录下的classed下
package mshtang.sql;
/**
* <p>Title: SQLWithAttacInsertAction</p>
* <p>Description: 当有附件的上载,SQL语句的构造适用本类。
* 前提:1.数据库字段名与附件上载域名相同;2.数据库中不实际保存附件,而只保存附件的存放路径;3.SmartUpload先初始化,即必须进行下列步骤:
* SmartUpload upload = new SmartUpload();
* upload.initalize(pageContext);
* upload.upload();这些步骤由类的使用者完成;4.在想得到SQL语句之前,
* 必须先设置文件的上载路径,即setDirectory("\\a\\b\\");可设置一级或
* 多极目录。该方法暂时用于windows平台</p>
* <p>Copyright: 厦门一方软件公司版权所有 (c) 2002</p>
* <p>Company: 厦门一方软件公司</p>
* @author 唐明胜
* @version 1.0
*/
import mshtang.StringAction;
import java.util.Enumeration;
import com.jspsmart.upload.*;
import javax.servlet.jsp.PageContext;
import javax.servlet.ServletContext;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
public class SQLAttachInsert
{
private String tempInsert;
private StringAction S;//本类中将使用到的字符串操作对象;
private String dir;//有外部使用者决定的附件保存路径;
private String[] attachPath;
private String[] attachField;
private String insert_Field;
private String insert_Value;
public SQLAttachInsert()
{
S = new StringAction();
}
public void setDirectory(String directory)
{
dir = directory;
}
/*上载附件,其实其与SQL语句构造无关,但为了保证数据库中
的保存值与实际存放路径一致,则必须有该前期工作*/
protected void uploadAttach(SmartUpload upload, PageContext pageContext)
{
ServletContext servletContext = pageContext.getServletContext();
String realPath = servletContext.getRealPath("/");
String relativePath = dir;
java.io.File javaFile = new java.io.File(realPath, relativePath);
try
{
if(!javaFile.exists())
{
javaFile.mkdirs();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
upload.save(relativePath);
}
catch(Exception e)
{
e.printStackTrace();
}
} /*设置附件信息:包括附件的保存字段以及给字段的值,每个字段可能多值,
其由spliter分开。
*/
private void setAttachmentInfo(SmartUpload upload, String newAttachFieldList, String spliter)//set the fieldname and the save path of the attachment
{
if(newAttachFieldList.equals(""))
{
attachField = null;
attachPath = null;
}
else
{
String[] attachFieldArray = S.stringSplit(newAttachFieldList, spliter);
String[] attachPathArray = new String[attachFieldArray.length];
com.jspsmart.upload.Files files = upload.getFiles();
Enumeration fileEnum = files.getEnumeration();
String fileDirectory;
String fileName;
String fieldName;
while(fileEnum.hasMoreElements())
{
fileDirectory = dir;
com.jspsmart.upload.File file = (com.jspsmart.upload.File)fileEnum.nextElement();
fileName = file.getFileName();
fieldName = file.getFieldName();
for(int i = 0; i < attachFieldArray.length; i++)
{
if(fieldName.equals(attachFieldArray[i]))
{
if(fileName != null && !fileName.equals(""))
{
fileDirectory += fileName;
if(attachPathArray[i] == null)
{
attachPathArray[i] = "";
}
if(!S.isContains(attachPathArray[i], fileDirectory))
{
attachPathArray[i] += fileDirectory + spliter;
}
}
}
}
}
attachField = attachFieldArray;//设置保存附件路径的数据库字段列表。
attachPath = attachPathArray;//设置上述字段的值列表。
}
}
/**功能:设置除附件外的其他域值相关信息:同样包括字段及其值
*
* @param upload:smartUpload对象;
* @param tableName:数据库表名;
* @param ignoreFieldList:欲忽略的其他域名列表;
* @param ignoreBegins:欲忽略的其他域名列表。
*/
private void setNormalFieldInfo(SmartUpload upload, String tableName, String ignoreFieldList, String ignoreBegins)//spliter的含义为如果某个域提交多值,则多值间采用spliter分隔。
{
if(tableName == null || tableName.equals(""))
{
return;
}
Enumeration nameEnum = upload.getRequest().getParameterNames();
String insertField = "insert into " + tableName + "(";
tempInsert = insertField;
String insertValue = "values (";
String temp_Field = insertField;
String temp_Value = insertValue;
int count = 0;
while(nameEnum.hasMoreElements())
{
String name = nameEnum.nextElement().toString().trim();
if(!ignoreFieldList.equals("") && S.isContains(ignoreFieldList, name))
{
continue;
}
if(!ignoreBegins.equals("") && name.startsWith(ignoreBegins))
{
continue;
}
String valueArray[] = upload.getRequest().getParameterValues(name);
String value = S.getArrayValue(valueArray, ";", true);
if(value != null && !value.equals(""))
{
insertField += name + ", ";
insertValue += "'" + value + "', ";
}
}
insert_Field = insertField;
insert_Value = insertValue;
}
/*将附件域与一般域构造的SQL组合起来*/
private String constructInsertSQL()
{
String[] attachPathArray = attachPath;
String[] attachFieldArray = attachField;
if(attachPathArray != null)
{
for(int i = 0; i < attachFieldArray.length; i++)
{
if(attachPathArray[i] != null && !attachPathArray[i].equals(dir))
{
insert_Field += attachFieldArray[i] + ", ";
insert_Value += "'" + attachPathArray[i] + "', ";
}
}
}
if(!insert_Field.equals(tempInsert))
{
insert_Field = insert_Field.trim();
insert_Field = insert_Field.substring(0, insert_Field.length() - 1) + ") ";
insert_Value = insert_Value.trim();
insert_Value = insert_Value.substring(0, insert_Value.length() - 1) + ")";
insert_Field += insert_Value;
}
else
{
insert_Field = "";
}
return insert_Field;
} /**功能:最后由外部调用,返回完整的SQL
*
* @param upload:SmartUpload对象;
* @param tableName:数据库表名;
* @param pageContext:页面环境对象;
* @param attachFieldList:欲上栽的附件域名列表;
* @param ignoreFieldList:欲忽略而不参与SQL构造的域列表;
* @param ignoreBegins:忽略以"ignoreBegins"开头所有的域名;
* @param spliter:上述列表的分隔字符串;
* @return
*/
public String getInsertSQL(SmartUpload upload, String tableName, PageContext pageContext, String attachFieldList, String ignoreFieldList, String ignoreBegins, String spliter)
{
uploadAttach(upload, pageContext);
setAttachmentInfo(upload, attachFieldList, spliter);
setNormalFieldInfo(upload, tableName, ignoreFieldList, ignoreBegins);
return constructInsertSQL();
} /**功能:方法重载。产生完整的插入语句insert SQL,默认分隔字符串是“;”。
*
* @param upload:SmartUpload对象;
* @param pageContext:页面环境对象;
* @param tableName:数据库表名;
* @param attachFieldList:欲上栽的附件域名列表;
* @param ignoreFieldList:欲忽略而不参与SQL构造的域列表;
* @param ignoreBegins:忽略以"ignoreBegins"开头所有的域名;
* @return
*/ public String getInsertSQL(SmartUpload upload, PageContext pageContext, String tableName, String attachFieldList, String ignoreFieldList, String ignoreBegins)
{
return getInsertSQL(upload, tableName, pageContext, attachFieldList, ignoreFieldList, ignoreBegins, ";");
}
/**功能:方法重载。产生完整的插入语句insert SQL,默认分隔字符串是“;”,
* 用于页面中欲忽略的域为规则的以“ignoreBegins”开始的域名,而不存在其他欲忽略的域名。
*
* @param upload:SmartUpload对象;
* @param pageContext:页面环境对象;
* @param tableName:数据库表名;
* @param attachFieldList:欲上栽的附件域名列表;
* @param ignoreBegins:忽略以"ignoreBegins"开头所有的域名;
* @return
*/
public String getInsertSQL(SmartUpload upload, PageContext pageContext, String tableName, String attachFieldList, String ignoreBegins)
{
return getInsertSQL(upload, tableName, pageContext, attachFieldList, "", ignoreBegins, ";");
} /**功能:方法重载。产生完整的插入语句insert SQL,用于没有需要忽略以特定字符串开头的域名。
*
* @param upload:SmartUpload对象;
* @param tableName:数据库表名;
* @param pageContext:页面环境对象;
* @param attachFieldList:欲上栽的附件域名列表;
* @param ignoreFieldList:欲忽略而不参与SQL构造的域列表;
* @param spliter:列表分隔字符串。
* @return
*/
public String getInsertSQL(SmartUpload upload, String tableName, PageContext pageContext, String attachFieldList, String ignoreFieldList, String spliter)
{
return getInsertSQL(upload, tableName, pageContext, attachFieldList, ignoreFieldList, "", spliter);
} /**功能:方法重载,多值间以分号分隔。
*
* @param upload:SmartUpload对象;
* @param tableName:数据库表名;
* @param pageContext:页面环境对象;
* @param attachFieldList:欲上栽的附件域名列表;
* @param ignoreFieldList:欲忽略而不参与SQL构造的域列表;
* @return
*/
public String getInsertSQL(SmartUpload upload, String tableName, PageContext pageContext, String attachFieldList, String ignoreFieldList)
{
return getInsertSQL(upload, tableName, pageContext, attachFieldList, ignoreFieldList, ";");
} /**功能:方法重载。没有需要忽略的域。
*
* @param upload:SmartUpload对象;
* @param tableName:数据库表名;
* @param pageContext:页面环境对象;
* @param attachFieldList:欲上栽的附件域名列表;
* @return
*/
public String getInsertSQL(SmartUpload upload, String tableName, PageContext pageContext, String attachFieldList)
{
return getInsertSQL(upload, tableName, pageContext, attachFieldList, "", ";");
} /**功能:方法重载。没有附件上传域。
*
* @param upload:SmartUpload对象;
* @param pageContext:页面环境对象;
* @param tableName:数据库表名;
* @param ignoreFieldList:欲忽略而不参与SQL构造的域列表;
* @return
*/
public String getinsertSQL(SmartUpload upload, PageContext pageContext, String tableName, String ignoreFieldList)
{
return getInsertSQL(upload, tableName, pageContext, "", ignoreFieldList, ";");
}
/**功能:方法重载。没有附件上载域以及需要忽略的域。功能同SQLNormalAction中的getInsertSQL(HttpServletRequest request, String tableName).
注意:该方法几乎无用
*
* @param upload
* @param tableName
* @param pageContext
* @return
*/
public String getInsertSQL(SmartUpload upload, String tableName, PageContext pageContext)
{
return getInsertSQL(upload, tableName, pageContext, "", "", ";");
}
}