问题描述
现在写好下载了,可以把f盘所有文件路径和文件名读入数据库,都是文件流下来的。但是想在网站上面直接显示图片,是不是只能直接链接的形式把图片放在工程目录下面啊,我能不能该一下下载的函数,让直接显示呢。附上我的下载函数:publicvoiddownloadfile(Strings,HttpServletResponseresponse)throwsSQLException,IOException//carefulthesamenamedifferentparent{con=ConMysql.gainConnection();try{sql=con.createStatement();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{rs=sql.executeQuery("SELECT*FROMimagewherefilename='"+s+"'");}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}StringfileName=s;rs.next();StringfilePath=rs.getString(2);filePath=filePath.replaceAll("/","\\");System.out.println(filePath);Filefile=newFile(filePath);if(!file.exists()){System.out.println(file.getAbsolutePath()+"文件不存在!");return;}//读取文件流//java.io.FileInputStreamFileInputStream=newjava.io.FileInputStream(file);//下载文件//设置响应头和下载保存的文件名response.setContentType("application/");response.setHeader("Location",fileName);response.setHeader("Content-Disposition","attachment;filename="+newString(fileName.getBytes("gb2312"),"iso8859-1"));OutputStreamoutputStream=response.getOutputStream();InputStreaminputStream=newFileInputStream(file);byte[]buffer=newbyte[1024];inti=-1;while((i=inputStream.read(buffer))!=-1){outputStream.write(buffer,0,i);}outputStream.flush();outputStream.close();inputStream.close();outputStream=null;}