当前位置: 技术问答>java相关
关于图片上传后的路径问题!急,在线等待!
来源: 互联网 发布时间:2015-08-19
本文导语: 文件上传的class中需要指定路径,我以前都是把路径设定死了,象下面这样: String uploadPath = "d:\orion\default-web-app\www\ittrade\merchandise\images\"; File dir = new File(uploadPath); 因为现在的环境随时要发生变化,我希望路径...
文件上传的class中需要指定路径,我以前都是把路径设定死了,象下面这样:
String uploadPath = "d:\orion\default-web-app\www\ittrade\merchandise\images\";
File dir = new File(uploadPath);
因为现在的环境随时要发生变化,我希望路径可以有该class自动获得,总是自动获得服务器中相关应用程序下的images目录,请问如何做?
String uploadPath = "d:\orion\default-web-app\www\ittrade\merchandise\images\";
File dir = new File(uploadPath);
因为现在的环境随时要发生变化,我希望路径可以有该class自动获得,总是自动获得服务器中相关应用程序下的images目录,请问如何做?
|
package mshtang.sql;
/**
*
*
*
*
* @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
/**
*
Title: SQLWithAttacInsertAction
*
Description: 当有附件的上载,SQL语句的构造适用本类。
* 前提:1.数据库字段名与附件上载域名相同;2.数据库中不实际保存附件,
* 而只保存附件的存放路径;3.SmartUpload先初始化,即必须进行下列步骤:
* SmartUpload upload = new SmartUpload();upload.initalize(pageContext);
* upload.upload();这些步骤由类的使用者完成;4.在想得到SQL语句之前,
* 必须先设置文件的上载路径,即setDirectory("\a\b\");可设置一级或
* 多极目录。该方法暂时用于windows平台
*
Copyright: 厦门一方软件公司版权所有 (c) 2002
*
Company: 厦门一方软件公司
* @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