上载文件并将其下载到数据库 [英] upload and download a file into database
本文介绍了上载文件并将其下载到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
LINUX UBUNTU操作系统
(JAVA SERVLET和JSP)
我想在Java中保存文件,但我不知道这些是否是用于保存示例(文本文件)的写代码
LINUX UBUNTU OS
(JAVA SERVLET AND JSP)
I want to save a file in java but i dont know if these is the write code for saving a sample(text file)
FileInputStream imgByte;
File imgLocation = new File(image_location);
stmt =con.prepareStatement(
"INSERT INTO image(image_id, image_data, image_name, image_ext)VALUES (?, ?, ?, ?);");
imgByte = new FileInputStream(imgLocation);
stmt.setInt(1, image_id+1);
stmt.setBinaryStream(2, (InputStream)imgByte, (int)(imgLocation.length()));
stmt.setString(3, image_name);
stmt.setString(4, image_ext);
stmt.executeUpdate();
我尝试使用它来保存图像,也有下载它的代码
I try it to save image and also I have a code for downloading it
String image_name = request.getParameter("image_name").trim();
int image_id = Integer.parseInt(request.getParameter("image_id").trim());
String filename = null;
Connection con=null;
try{
Class.forName("org.postgresql.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"postgres","postgres");
Statement st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("Select * from image where image_name=$$"+image_name+"$$ and image_id="+image_id+";");
while(rs1.next()){
filename = image_name+"."+rs1.getString("image_ext");
}
String strQuery = "select image_data from image where image_name=$$"+image_name+"$$ and image_id="+image_id+"";
rs1 = st1.executeQuery(strQuery);
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
}
rs1 = st1.executeQuery(strQuery);
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
readImg.read(rb, 0, len);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.setHeader("Content-disposition","attachment; filename=" +filename);
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
我尝试保存文本文件并下载
当我打开文件时出现错误...
gedit无法检测到字符编码.
请检查您是否未尝试打开二进制文件.
从菜单中选择一个字符编码,然后重试.
I try saving a text file and download it
when i open the file theres an error...
gedit has not been able to detect the character coding.
Please check that you are not trying to open a binary file.
Select a character coding from the menu and try again.
推荐答案
"+ image_name +"
"+image_name+"
和image_id ="+ image_id +" ;); while(rs1.next()){ filename = image_name +." + rs1.getString("image_ext"); } 字符串strQuery =从图像中选择图像数据,其中image_name =
and image_id="+image_id+";"); while(rs1.next()){ filename = image_name+"."+rs1.getString("image_ext"); } String strQuery = "select image_data from image where image_name=
" + image_name +"
"+image_name+"
这篇关于上载文件并将其下载到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文