java web批量上传图片_Java web图片上传和文件上传

图片上传和文件上传本质上是一样的,图片本身也是文件。文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作。

注意事项

1.form表单一定要写属性enctype="multipart/form-data"

2.为了能保证文件能上传成功file控件的name属性值要和你提交的控制层变量名一致,

例如空间名是file那么你要在后台这样定义

private File file; //file控件名

private String fileContentType;//图片类型

private String fileFileName; //文件名

1.jsp页面

pageEncoding="UTF-8"%>

文件上传

1.页面数据需要提交的Controller

package com.cpsec.tang.chemical.action;import java.io.File;import java.io.IOException;import java.util.Random;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import org.springframework.stereotype.Controller;import com.cpsec.tang.chemical.biz.LunboBiz;import com.cpsec.tang.chemical.entity.Image;import com.opensymphony.xwork2.ActionSupport;

@Controller("lunboAction")public class LunboAction extends ActionSupport {    /**

*

*/

private static final long serialVersionUID = 1L;

@Resource(name="lunboBiz")    private LunboBiz lunboBiz;    private Image image;    private File file; //file控件名

private String fileContentType;//图片类型

private String fileFileName; //文件名

private Integer number;

public String findImage(){

image=lunboBiz.findImage();        return SUCCESS;

}

public String alterImage(){

image=lunboBiz.findImage();        return SUCCESS;

}

public String alterImage1(){

HttpServletRequest request = ServletActionContext.getRequest();

String root = request.getRealPath("/upload");//图片要上传到的服务器路径

String names[]=fileFileName.split("\\.");

String fileName="";        if(names.length>=1){

fileName=getRandomString(20)+"."+names[names.length-1];

}

String picPath="upload/"+fileName;//图片保存到数据库的路径

File file1=new File(root);        try {

FileUtils.copyFile(file, new File(file1,fileName));

}

} catch (IOException e) {

e.printStackTrace();

}        return SUCCESS;

}

/*获取一条随机字符串*/

public String getRandomString(int length) { //length表示生成字符串的长度

String base = "abcdefghijklmnopqrstuvwxyz0123456789";

Random random = new Random();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < length; i++) {

int number = random.nextInt(base.length());

sb.append(base.charAt(number));

}

return sb.toString();

}

}

这是通过复制的方式上传文件,还有其他方式

方法二

@Controller("contractAction")public class ContractAction extends ActionSupport {

private final static String UPLOADDIR = "/files";//文件上传的路径,在webContent下建立

private File file; //input控件名一定为file

//上传文件名集合

private String fileFileName;

//上传文件内容类型集合

private String fileContentType;

private String filename;

public String upload() throws FileNotFoundException, IOException{

String path=uploadFile();//文件保存数据库的路径

return SUCCESS;

}

//执行上传功能

@SuppressWarnings("deprecation")    public String uploadFile() throws FileNotFoundException, IOException {

try {

InputStream in = new FileInputStream(file);

String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);

File fileLocation = new File(dir);

//此处也可以在应用根目录手动建立目标上传目录

if(!fileLocation.exists()){

boolean isCreated = fileLocation.mkdir();

if(!isCreated) {

//目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。

return null;

}

}           // this.setFileFileName(getRandomString(20));

String[] Name=this.getFileFileName().split("\\.");

String fileName=getRandomString(20)+"."+Name[Name.length-1];            this.setFileFileName(fileName);

System.out.println(fileName);

File uploadFile = new File(dir, fileName);

OutputStream out = new FileOutputStream(uploadFile);

byte[] buffer = new byte[1024 * 1024];

int length;

while ((length = in.read(buffer)) > 0) {

out.write(buffer, 0, length);

}

in.close();

out.close();

return UPLOADDIR.substring(1)+"\\"+fileFileName;

} catch (FileNotFoundException ex) {                return null;

} catch (IOException ex) {                return null;

}

}

public static String getRandomString(int length){

String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Random random=new Random();

StringBuffer sb=new StringBuffer();        for(int i=0;i

sb.append(str.charAt(number));

}        return sb.toString();

}

}

除了单图上传还有多图上传,原理都是一样的

package com.cpsec.tang.chemical.action;import java.io.File;  import java.io.FileInputStream;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/**

* 多文件上传 */public class FilesUploadAction extends ActionSupport {         //上传文件存放路径

private final static String UPLOADDIR = "/upload";

//上传文件集合

private List file;

//上传文件名集合

private List fileFileName;

//上传文件内容类型集合

private List fileContentType;

public List getFile() {

return file;

}

public void setFile(List file) {

this.file = file;

}

public List getFileFileName() {

return fileFileName;

}

public void setFileFileName(List fileFileName) {

this.fileFileName = fileFileName;

}

public List getFileContentType() {

return fileContentType;

}

public void setFileContentType(List fileContentType) {

this.fileContentType = fileContentType;

}

public String uploadform() throws Exception {

HttpServletRequest request = ServletActionContext.getRequest();

String webpath=null;//上传路径

for (int i = 0; i < file.size(); i++) {

//循环上传每个文件                   uploadFile(i);

webpath="upload/"+this.getFileFileName().get(i);

}             return "SUCCESS";

}

//执行上传功能

private String uploadFile(int i) throws FileNotFoundException, IOException {

try {

InputStream in = new FileInputStream(file.get(i));

String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);

File fileLocation = new File(dir);

//此处也可以在应用根目录手动建立目标上传目录

if(!fileLocation.exists()){

boolean isCreated = fileLocation.mkdir();

if(!isCreated) {

//目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。

return null;

}

}

String fileName=this.getFileFileName().get(i);

File uploadFile = new File(dir, fileName);

OutputStream out = new FileOutputStream(uploadFile);

byte[] buffer = new byte[1024 * 1024];

int length;

while ((length = in.read(buffer)) > 0) {

out.write(buffer, 0, length);

}

in.close();

out.close();

return uploadFile.toString();

} catch (FileNotFoundException ex) {                 return null;

} catch (IOException ex) {                 return null;

}

}

}

郑重声明:资源都是网络上的,如有侵权,联系我删除。

懂得分享才能学好Java,

Java开源有你的一份力量

你有好的资源可以分享给我;

记得写上你的名字,发表时会署名

邮箱:lhforyourfuture@163.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值