怎么样用http把图片上传到服务器的指定目录

时间:2021-12-20 12:25:57
我现在用的是直接把图片上传保存在ORACLE数据库BLOB字段中,但这种方法对数据库压力太大且在我这写的程序中如果上传图片过大就出现插入异常。我希望把客户端的图片直接上传到服务器上指定的目录,而在数据库里面只保存图片路径。
如果这样做程序要怎么样写?望有这样经验的朋友给出例子或代码,谢谢!!!

6 个解决方案

#1



  String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
            InputStream stream = fileone.getInputStream() ; //把文件读入
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            
            /*
             * 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
             */
            OutputStream bos = new FileOutputStream(filePath + fileone.getFileName()) ;
            
            request.setAttribute("fileName",filePath + "/" + fileone.getFileName() ) ;
            int bytesRead = 0 ;
            byte[] buffer = new byte[8192] ;
            while( (bytesRead = stream.read(buffer,0,8192) ) != -1  )
            {
                bos.write(buffer,0,bytesRead) ;
            }
            
            bos.close();
            stream.close() ;    
            
            //上传文件完成
            String oldurl= filePath + fileone.getFileName() ;
            String newurl= filePath + "min_" + fileone.getFileName() ;  //新的缩略图保存地址,这里就可以自己保存到数据库了

#2


我要如何获取下面Content-Disposition中的filename的值,因为我不知道你的fileone.getFileName()是怎么样实现的,能给出代码吗?谢谢!!!
再者我能怎样分割下面这段信息?


-----------------------------7d72fd5607ca
Content-Disposition: form-data; name="FILE1"; filename="C:\Documents and Settings\yuxizhou\桌面\test\332.TXT"
Content-Type: text/plain

  String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
            InputStream stream = fileone.getInputStream() ; //把文件读入
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            
            /*
             * 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
             */
            OutputStream bos = new FileOutputStream(filePath + fileone.getFileName()) ;
            
            request.setAttribute("fileName",filePath + "/" + fileone.getFileName() ) ;
            int bytesRead = 0 ;
            byte[] buffer = new byte[8192] ;
            while( (bytesRead = stream.read(buffer,0,8192) ) != -1  )
            {
                bos.write(buffer,0,bytesRead) ;
            }
            
            bos.close();
            stream.close() ;    
            
            //上传文件完成
            String oldurl= filePath + fileone.getFileName() ;
            String newurl= filePath + "min_" + fileone.getFileName() ;  //新的缩略图保存地址,这里就可以自己保存到数据库了


-----------------------------7d72fd5607ca--

#3


如果你用STRUTS的话,可以直接用他的上传文件类库
import org.apache.struts.upload.*;

#4


我们没有用框架,请给出分割的方法,谢谢!!!

#5


private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(" . ");
if (pos > -1) {
return fileName.substring(pos);
} else {
return fileName;
}
}

#6


还没用过 struts的上传类库哦   看看啊 呵呵!!

#1



  String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
            InputStream stream = fileone.getInputStream() ; //把文件读入
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            
            /*
             * 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
             */
            OutputStream bos = new FileOutputStream(filePath + fileone.getFileName()) ;
            
            request.setAttribute("fileName",filePath + "/" + fileone.getFileName() ) ;
            int bytesRead = 0 ;
            byte[] buffer = new byte[8192] ;
            while( (bytesRead = stream.read(buffer,0,8192) ) != -1  )
            {
                bos.write(buffer,0,bytesRead) ;
            }
            
            bos.close();
            stream.close() ;    
            
            //上传文件完成
            String oldurl= filePath + fileone.getFileName() ;
            String newurl= filePath + "min_" + fileone.getFileName() ;  //新的缩略图保存地址,这里就可以自己保存到数据库了

#2


我要如何获取下面Content-Disposition中的filename的值,因为我不知道你的fileone.getFileName()是怎么样实现的,能给出代码吗?谢谢!!!
再者我能怎样分割下面这段信息?


-----------------------------7d72fd5607ca
Content-Disposition: form-data; name="FILE1"; filename="C:\Documents and Settings\yuxizhou\桌面\test\332.TXT"
Content-Type: text/plain

  String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
            InputStream stream = fileone.getInputStream() ; //把文件读入
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            
            /*
             * 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
             */
            OutputStream bos = new FileOutputStream(filePath + fileone.getFileName()) ;
            
            request.setAttribute("fileName",filePath + "/" + fileone.getFileName() ) ;
            int bytesRead = 0 ;
            byte[] buffer = new byte[8192] ;
            while( (bytesRead = stream.read(buffer,0,8192) ) != -1  )
            {
                bos.write(buffer,0,bytesRead) ;
            }
            
            bos.close();
            stream.close() ;    
            
            //上传文件完成
            String oldurl= filePath + fileone.getFileName() ;
            String newurl= filePath + "min_" + fileone.getFileName() ;  //新的缩略图保存地址,这里就可以自己保存到数据库了


-----------------------------7d72fd5607ca--

#3


如果你用STRUTS的话,可以直接用他的上传文件类库
import org.apache.struts.upload.*;

#4


我们没有用框架,请给出分割的方法,谢谢!!!

#5


private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(" . ");
if (pos > -1) {
return fileName.substring(pos);
} else {
return fileName;
}
}

#6


还没用过 struts的上传类库哦   看看啊 呵呵!!