RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java文件照片上传代码 java上传图片到tomcat

解释一下这段JAVA 关于图片上传的代码

private File file;

成都创新互联主要从事成都网站设计、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务安次,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

private String fileFileName;

private String picture;

//都有getter 和 setter

InputStream is = new FileInputStream(file);

//引入一个IO流的输入流

String root = ServletActionContext.getRequest()

.getRealPath("/bookpicture");

//通过REQUEST来得到相对地址,并在后面加上/bookpicture

File f = new File(root, this.getFileFileName());

//定义一个FILE文件,第一个参数是文件的路径,第二个是文件的名字

picture="."+"\\"+"bookpicture"+"\\"+this.getFileFileName();

//为PICTURE字符串赋值,/地址/文件名

System.out.println

("======picture====="+picture);

//从控制台输出Picture

OutputStream os = new FileOutputStream(f);

//第一个文件的输出流

byte[] buffer = new byte[1024];

//定义一个bufer的字符串,长度为1024

int len = 0;

while ((len = is.read(buffer)) 0) {

//如果从制定文件中读取到的信息为结束就继续循环

os.write(buffer, 0, len);

//将文件读出的内容写入到指定的文件中

}

java 中如何向服务器上传图片

我们使用一些已有的组件帮助我们实现这种上传功能。

常用的上传组件:

Apache 的 Commons FileUpload

JavaZoom的UploadBean

jspSmartUpload

以下,以FileUpload为例讲解

1、在jsp端

form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data"

要注意enctype="multipart/form-data"

然后只需要放置一个file控件,并执行submit操作即可

input name="file" type="file" size="20"

input type="submit" name="submit" value="提交"

2、web端

核心代码如下:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

DiskFileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

try {

List items = upload.parseRequest(request);

Iterator itr = items.iterator();

while (itr.hasNext()) {

FileItem item = (FileItem) itr.next();

if (item.isFormField()) {

System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));

} else {

if (item.getName() != null !item.getName().equals("")) {

System.out.println("上传文件的大小:" + item.getSize());

System.out.println("上传文件的类型:" + item.getContentType());

System.out.println("上传文件的名称:" + item.getName());

File tempFile = new File(item.getName());

File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());

item.write(file);

request.setAttribute("upload.message", "上传文件成功!");

}else{

request.setAttribute("upload.message", "没有选择上传文件!");

}

}

}

}catch(FileUploadException e){

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

request.setAttribute("upload.message", "上传文件失败!");

}

request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);

}

java图片批量上传代码?

用struts也可以实现 多文件上传

下面是我写的代码,作为参考!

/*文件目录*/

public static String [] fileArray={

"logo.png",

"index.swf",

"OEMInfo.txt",

"favicon.ico"};

/**

* @author Caoshun

* @see 接收并保存文件

* */

public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){

String fileName="";

//获取表单中的文件资源

HashtableObject, Object files = form.getMultipartRequestHandler().getFileElements();

//遍历文件,并且循环保存

//当前处理文件序号

int file_num=1;

for (EnumerationObject e = files.keys(); e.hasMoreElements();) {

/*根据处理的当前文件下标,确定文件名*/

fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());

if (file != null file.getFileSize() 0) {

try {

//使用formfile.getInputStream()来获取一个文件的输入流进行保存。

//文件名

//String fileName = file.getFileName();

//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);

//文件大小

//int fileSize = file.getFileSize();

//文件流

InputStream is = file.getInputStream();

//将输入流保存到文件

//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中写入

File rf = new File(rootPath1);

FileOutputStream fos = null;

fos = new FileOutputStream(new File(rf, fileName));

byte[] b = new byte[10240];

int real = 0;

real = is.read(b);

while (real 0) {

fos.write(b, 0, real);

real = is.read(b);

}

//往en中写入

File rf2 = new File(rootPath2);

InputStream is2 = file.getInputStream();

FileOutputStream fos2 = null;

fos2 = new FileOutputStream(new File(rf2, fileName));

byte[] b2 = new byte[10240];

int real2 = 0;

real2 = is2.read(b2);

while (real2 0) {

fos2.write(b2, 0, real2);

real2 = is2.read(b2);

}

//关闭文件流

fos.close();

is.close();

fos2.close();

is2.close();

} catch (RuntimeException e1) {

e1.printStackTrace();

} catch (Exception ee) {

ee.printStackTrace();

}

file.destroy();

}

file_num++;

}

}


本文名称:java文件照片上传代码 java上传图片到tomcat
标题URL:http://scyingshan.cn/article/ddepjdj.html