b2c信息网

您现在的位置是:首页 > 热点问题 > 正文

热点问题

java文件上传源码(java文件上传代码)

hacker2022-08-23 15:45:19热点问题91
本文目录一览:1、java上传文件代码2、求:java文件上传源码(全)

本文目录一览:

java上传文件代码

public class FileUpLoad extends ActionSupport{

//"多文件上传就用list就可以了private ListFile file;"

private File file;

//上传文本的name

public File getFile() {

return file;

}

public void setFile(File file) {

this.file = file;

}

private String fileContentType;

//上传的文件类型。

public String getFileContentType() {

return fileContentType;

}

public void setFileContentType(String fileContentType) {

this.fileContentType = fileContentType;

}

//获取上传文件的名称

private String fileFileName;

public String getFileFileName() {

return fileFileName;

}

public void setFileFileName(String fileFileName) {

this.fileFileName = fileFileName;

}

public String upload() throws Exception

{

//获取文件上传路径

String root=ServletActionContext.getRequest().getRealPath("/upload");

InputStream is=new FileInputStream(file);

String.substring(fileFileName.indexOf("."));//截取上传文件的后缀。便于新定义名称。.jpg

System.out.println(name);

File descFile=new File(root,新定义的文件名称+fileFileName.indexOf("."));

OutputStream os=new FileOutputStream(descFile);

byte[] buffer=new byte[1024];

int length=0;

while(-1!=(length=(is.read(buffer))))

{

os.write(buffer, 0, length);

}

is.close();

os.close();

return SUCCESS;

}

}

求:java 文件上传源码(全)

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import org.apache.commons.fileupload.*;

import org.apache.commons.fileupload.servlet.*;

import org.apache.commons.fileupload.disk.*;public class UploadServlet extends HttpServlet {

private String filePath; //存放上传文件的目录

private String tempFilePath; //存放临时文件的目录 public void init(ServletConfig config)throws ServletException {

super.init(config);

filePath=config.getInitParameter("filePath");

tempFilePath=config.getInitParameter("tempFilePath");

filePath=getServletContext().getRealPath(filePath);

tempFilePath=getServletContext().getRealPath(tempFilePath);

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/plain");

//向客户端发送响应正文

PrintWriter outNet=response.getWriter();

try{

//创建一个基于硬盘的FileItem工厂

DiskFileItemFactory factory = new DiskFileItemFactory();

//设置向硬盘写数据时所用的缓冲区的大小,此处为4K

factory.setSizeThreshold(4*1024);

//设置临时目录

factory.setRepository(new File(tempFilePath)); //创建一个文件上传处理器

ServletFileUpload upload = new ServletFileUpload(factory);

//设置允许上传的文件的最大尺寸,此处为4M

upload.setSizeMax(4*1024*1024);

List /* FileItem */ items = upload.parseRequest(request); Iterator iter = items.iterator();

while (iter.hasNext()) {

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

if(item.isFormField()) {

processFormField(item,outNet); //处理普通的表单域

}else{

processUploadedFile(item,outNet); //处理上传文件

}

}

outNet.close();

}catch(Exception e){

throw new ServletException(e);

}

} private void processFormField(FileItem item,PrintWriter outNet){

String name = item.getFieldName();

String value = item.getString();

outNet.println(name+":"+value+"\r\n");

}

private void processUploadedFile(FileItem item,PrintWriter outNet)throws Exception{

String filename=item.getName();

int index=filename.lastIndexOf("\\");

filename=filename.substring(index+1,filename.length());

long fileSize=item.getSize();

if(filename.equals("") fileSize==0)return; File uploadedFile = new File(filePath+"/"+filename);

item.write(uploadedFile);

outNet.println(filename+" is saved.");

outNet.println("The size of " +filename+" is "+fileSize+"\r\n");

}

}

java 文件上传的代码,尽量详细一点。。。

// 这是我写的一个方法,里面只需要传两个参数就OK了,在任何地方调用此方法都可以文件上传

/**

* 上传文件

* @param file待上传的文件

* @param storePath待存储的路径(该路径还包括文件名)

*/

public void uploadFormFile(FormFile file,String storePath)throws Exception{

// 开始上传

InputStream is =null;

OutputStream os =null;

try {

is = file.getInputStream();

os = new FileOutputStream(storePath);

int bytes = 0;

byte[] buffer = new byte[8192];

while ((bytes = is.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytes);

}

os.close();

is.close();

} catch (Exception e) {

throw e;

}

finally{

if(os!=null){

try{

os.close();

os=null;

}catch(Exception e1){

;

}

}

if(is!=null){

try{

is.close();

is=null;

}catch(Exception e1){

;

}

}

}

}

跪求java上传文件的原代码;谢谢;

package web;

import java.io.File;

import java.io.IOException;

import java.net.URLDecoder;

import java.util.List;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileLoadServlet extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//创建工厂类实例

DiskFileItemFactory factory=new DiskFileItemFactory();

ServletFileUpload sfu=new ServletFileUpload(factory);//创建解析器

try {

//FileItem对象封装了一个表单单中的所有的数据

ListFileItem items=sfu.parseRequest(request);

//通过FileItem对象提供的方法来获得表单中的数据

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

FileItem item=items.get(i);

if(item.isFormField()){

//除了input type="file"之外的表单域

String value=item.getString();

System.out.println(value);

}else{

ServletContext sctx=getServletContext();

String path=sctx.getRealPath("upload");

System.out.println("path:"+path);

//获得上传文件名称

String fileName=item.getName();

File file=new File(path+"\\"+URLDecoder.decode(fileName, "GBK"));

item.write(file);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

这是浏览器端上传到服务器的

发表评论

评论列表

  • 礼忱徒掠(2022-08-23 20:59:32)回复取消回复

    SUCCESS;}}求:java 文件上传源码(全)import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;impo

  • 野欢殊姿(2022-08-24 01:27:56)回复取消回复

    ter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next();

  • 北槐辞慾(2022-08-23 23:13:20)回复取消回复

    (config); filePath=config.getInitParameter("filePath"); tempFilePath=config.getInitP