javaweb上传图片(javaweb上传图片到数据库保存)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享javaweb上传图片的知识,其中也会对javaweb上传图片到数据库保存进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、java web开发,上传图片并读取
- 2、java web项目上传一些图片,必须刷新文件夹才能显示图片,如何编写自动刷新代码??
- 3、java web怎么上传图片到指定的项目文件夹,并获取该路径,将路径存储到数据库,用相对路径哦
- 4、用Java Web的jsp制作图片上传和显示如何实现
java web开发,上传图片并读取
java web开发中,使用文件操作类来上传图片并读取,如下代码:
* @desc: 图片处理工具
* @author: bingye
* @createTime: 2015-3-17 下午04:25:32
* @version: v1.0
*/
public class ImageUtil {
/**
* 将图片写到客户端
* @author: bingye
* @createTime: 2015-3-17 下午04:36:04
* @history:
* @param image
* @param response void
*/
public static void writeImage(byte[] image,HttpServletResponse response){
if(image==null){
return;
}
byte[] buffer=new byte[1024];
InputStream is=null;
OutputStream os=null;
try {
is=new ByteArrayInputStream(image);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(is!=null){is.close();}
if(os!=null){os.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取指定路劲图片
* @author: bingye
* @createTime: 2015-3-21 上午10:50:44
* @param filePath
* @param response void
*/
public static void writeImage(String filePath,HttpServletResponse response){
File imageFile=new File(filePath);
if(imageFile!=null imageFile.exists()){
byte[] buffer=new byte[1024];
InputStream is=null;
OutputStream os=null;
try {
is=new FileInputStream(imageFile);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
腊散郑 轮颂 os.write(buffer);
os.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(is!=null){is.close();}
if(os!=null){os.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 图片上传到文件夹
* @author: bingye
* @createTime: 2015-3-20 下午08:07:25
* @param file
* @param savePath
* @return boolean
*/
public static ResultDto uploadToLocal(CommonsMultipartFile file,String savePath){
if(file!=null !file.isEmpty()){
//获取文件名称
String fileName=file.getOriginalFilename();
//获取后缀名
String suffixName=fileName.substring(fileName.indexOf(".")+1);
//新名称
String newFileName=System.currentTimeMillis()+"."+suffixName;
//新文件路劲
String filePath=savePath+newFileName;
//获取存储文件路径
File fileDir=new File(savePath);
掘孝 if(!fileDir.exists()){
//如果文件夹没有:新建
fileDir.mkdirs();
}
FileOutputStream fos=null;
try {
fos=new FileOutputStream(filePath);
fos.write(file.getBytes());
fos.flush();
return ResultUtil.success("UPLOAD_SUCCESS", URLEncoder.encode(newFileName,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
return ResultUtil.fail("UPLOAD_ERROR");
} finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
return ResultUtil.fail("UPLOAD_ERROR");
}
}
}
return ResultUtil.fail("UPLOAD_ERROR");
}
}
java web项目上传一些图片,必须刷新文件夹才能显示图片,如何编写自动刷新代码??
图片不会自动显示是不是浏览器缓存问题~?就是譬如两次请求都是同一个地址,但是如果你存储的图片已经更改,但是名字没变,这样浏览器貌似是不会重新发出请举尘销求。你兄颤可以在请求时候附加一个随机数,
或者你正游可以再详细描述下,你说的文件夹是在哪里的文件夹,网络上的?~还是本地?你说显示图片是在哪里显示?页面上?还是本地文件夹里面?

java web怎么上传图片到指定的项目文件夹,并获取该路径,将路径存储到数据库,用相对路径哦
当前项目的路经,通过request.getSession().getServletContext().getRealPath("/"世禅)获取此碰到搜扒尘的
用Java Web的jsp制作图片上传和显示如何实现
用jspSmartUpload组件亏友来实现,用jsp+servlet在Servlet里实现唤凯的代码:
PrintWriter out = response.getWriter();
int count = 0;
// 实例化上传控件对象
SmartUpload su = new SmartUpload();
// 初始化操作
su.initialize(config, request, response);
// 设置上传文件最大字节数
su.setTotalMaxFileSize(100000);
//
try {
//禁止上传指定扩展名的文件
su.setDeniedFilesList("ext,bat,jsp");
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
// 上传文件到服务器
su.upload();
File fileup = new File(request.getRealPath("upload"));
if(!fileup.exists()){
// 创建目录
fileup.mkdir();
}
// 处理多个文件的上传
for(int i = 0;i su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if(!file.isMissing()){ // 如果文件有效
// 保存文件到指定上传目录
file.saveAs("/upload/new."+file.getFileExt(), su.SAVE_VIRTUAL);
count = su.save("/upload");
}
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
out.println(count +"file(s) uploaded");
如果你对这个上传组件不了解,最好是先去查查和空唤用法。。。
javaweb上传图片的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaweb上传图片到数据库保存、javaweb上传图片的信息别忘了在本站进行查找喔。
