各位,做的个人简历有照片上传问题??急

java_jin 2002-09-06 11:18:58
我用的win2000+tomcat4.0+jdk1.3 数据库是oracle8.05,
做的职工简历,附带照片,怎么实现照片传到数据库字段,
如果有完整的代码,肯定给分!!!!!谢谢各位
...全文
683 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xycleo 2002-09-06
  • 打赏
  • 举报
回复
什么都能上传
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.neusoft.ExtendLibraries.ExtendConfig.configInfo;

public class ImportServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final int ONE_MEG = 2048*1024;

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
int buffsize = 1024*8;
int contentLength = request.getContentLength();
// Hashtable pageItems = null;
// value chosen to limit denial of service - 2 MB, in this case
if (contentLength > ONE_MEG){
System.out.println ("contentLength > ONE_MEG");
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html><head><title>Too big</title></head>");
out.println("<body><h1>错误 - 文件大于2M!");
out.println("</h1></body></html>");
out.close();
}
else{
HttpSession session = request.getSession(true);
int status=0;//status=0:非正文;status=1:分隔符;status=2:文件内容;status=3:其它内容
String ContentType=request.getHeader("content-type");
String boundary=ContentType.substring(ContentType.indexOf("boundary=")+9);
String fileName="";
String itemName="";
String lastItem="";
Properties pp = new Properties();
try{
pp = configInfo.getConfigObj();
}catch(Exception e){

}

String strPath=pp.getProperty("OutputPath");// "D:\\Application_Studio\\JTP\\lib\\";
ServletInputStream in = request.getInputStream();
byte[] b = new byte[buffsize];
byte[] byContent = new byte[contentLength];
int result;
int totalRead = 0;
FileOutputStream fileout = null;
try{
result = in.readLine(b, 0, b.length);
while (result != -1){
String rowStr=new String(b,0,result);
if (IsBoundary(rowStr,boundary)){ //遇到分割符
switch(status){
case 0:break;
case 1:break;
case 2:
if ( totalRead!=0&!fileName.equals("")){
fileout =new FileOutputStream(strPath+fileName);
fileout.write(byContent, 0, totalRead);
totalRead=0 ;
}
break;
case 3:
String itemContent=new String(byContent,0,totalRead);
if (itemName.equals(lastItem)){
String sval =(String) session.getAttribute (itemName);
sval+=";"+ itemContent;
session.setAttribute(itemName, sval);
}
else{
session.setAttribute(itemName, itemContent);
}
lastItem=itemName;
break;
}
totalRead=0;
status=0;
}
else if ( status<2 ){ //非正文内容
if (IsFile(rowStr)){
fileName=getFilename(rowStr);
if (!fileName.equals("")){
session.setAttribute("filename", "<A href=http://192.168.66.87:8080/upload/"+ fileName+">"+ fileName+"</A>");
}

result = in.readLine(b, 0, buffsize);//跳过冗余信息
result = in.readLine(b, 0, buffsize);//跳过冗余信息
status=2;
}
else if (IsElement(rowStr)){
itemName=getElename(rowStr);
status=3;
}
}
else { //上载文件内容或表单提交的数据
for ( int i=0; i<result; i++ ){
byContent[totalRead+i]=b[i];
}
totalRead+=result;
}

result = in.readLine(b, 0, buffsize);

}
fileout.close();
}catch (IOException ioe){
System.out.println("IOException at Import File:"+ioe.getMessage());
ioe.printStackTrace();
}
}//end if (contentLength > ONE_MEG)
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head><title>upload</title></head>");
out.println("<body>");
out.println("文件上传成功!");
out.println("</body></html>");
out.close();
}//end doPost()

public String getServletInfo(){
return "A servlet that shows the request headers and body sent by the client";
}
public boolean IsBoundary(String str,String boundary){
if(str.length() < 40 & str.length() > 60 ) return false;
if(str.indexOf(boundary)!=-1) return true;
else return false;
}
public boolean IsFile(String str){
if(str.length() < 53 & str.length() >253) return false;
if(str.indexOf("filename=")!=-1) return true;
else return false;
}
public boolean IsElement(String str){
if(str.length() < 40 & str.length() >253) return false;
if(str.indexOf("name=")!=-1) return true;
else return false;
}
public String getFilename(String str){
String pathName=str.substring(str.indexOf("filename="));
int startIndex=pathName.lastIndexOf("\\")+1;
int endIndex=pathName.length()-3;
return pathName.substring(startIndex,endIndex);
}
public String getElename(String str){
String tempStr=str.substring(str.indexOf("name=\"")+6);
return tempStr.substring(0,tempStr.indexOf("\""));
}

//Clean up resources
public void destroy() {
}
}
zhu_liping 2002-09-06
  • 打赏
  • 举报
回复
就是图片上传,再读出显示
悠悠的爸爸 2002-09-06
  • 打赏
  • 举报
回复
续:
/**功能:最后由外部调用,返回完整的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, "", "", ";");
}

public synchronized String getFlowID(Connection conn, String tableName, String IDName)
{
SQLNormalAction sqlBean = new SQLNormalAction();
return sqlBean.getFlowID(conn, tableName, IDName);
}
}
悠悠的爸爸 2002-09-06
  • 打赏
  • 举报
回复
我有一个bean,可同时上载文件并获得sql语句,只是文件上载到了jsp服务器的某个目录下,而没有保存在数据库的字段中,不够安全。对你不知有没有用。

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 mshtang.sql.SQLNormalAction;
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;
}
利而不害 2002-09-06
  • 打赏
  • 举报
回复
to xycleo()虚竹和尚() :
ImportServlet.java:5: 不能解析符号
符号:类 configInfo
位置:包 in ExtendConfig
import com.neusoft.ExtendLibraries.ExtendConfig.configInfo;

81,092

社区成员

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

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