导航:首页 > 编程语言 > java文件上传源码

java文件上传源码

发布时间:2023-08-12 15:31:13

A. java中怎么把文件上传到服务器的指定路径

文件从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。

java中文件上传到服务器的指定路径的代码:

在前台界面中输入:

<form method="post" enctype="multipart/form-data" action="../manage/excelImport.do">

请选文件:<input type="file" name="excelFile">

<input type="submit" value="导入" onclick="return impExcel();"/>

</form>

action中获取前台传来数据并保存

/**

* excel 导入文件明颂

* @return

* @throws IOException

*/

@RequestMapping("/usermanager/excelImport.do")

public String excelImport(

String filePath,

MultipartFile excelFile,HttpServletRequest request) throws IOException{

log.info("<<悉孙<<<<action:{} Method:{} start>>>>>>","usermanager","excelImport" );

if (excelFile != null){

String filename=excelFile.getOriginalFilename();

String a=request.getRealPath("u/cms/www/201509");

SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服激陆郑务器的路径

}

log.info("<<<<<<action:{} Method:{} end>>>>>>","usermanager","excelImport" );

return "";

}

/**

* 将MultipartFile转化为file并保存到服务器上的某地

*/

public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException

{

FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);

System.out.println("------------"+path + "/"+ savefile);

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

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}

B. java带参带进度条(真实)上传文件代码

思路如下:


public class MyClass extends Thread {

public JProgressBar bar = new JProgressBar();//进度条
public OutputStream os;// 通向服务器的流
public InputStream is;// 你的文件流
public long len;// 你文件的总长度

public void run() {

byte[] buff = new byte[1024];
long passedlen = 0;
try {
int mark = 0;
while (true) {
mark = is.read(buff, 0, 1024);
os.write(buff, 0, mark);
// 更新进度
passedlen += mark;
int prog = (int) ((double) passedlen / (len == 0 ? 1 : len) * 100);
// 改变进度条的value值
bar.setValue(prog);
// 结束判定
if (passedlen == len) {
break;
}
}
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}


C. java如何实现文件上传

public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize - receiveLen > bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen < fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return receiveLen;
}
这个方法从InputStream中读取内容,写到OutputStream中。
那么发送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至于存到数据库里嘛,Oracle里用Blob。搜索一下,也是一样的。从Blob能获取一个输出流。

D. 用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv

package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 完成文件上传 (不是解析上传内容,因为上传内容 由fileUpload拦截器负责解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上传内容
// <input type="file" name="upload" />
private File upload; // 这里变量名 和 页面表单元素 name 属性一致
private String uploadContentType;
private String uploadFileName;

public void setUpload(File upload) {
this.upload = upload;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
if (upload == null) { // 通过xml配置 required校验器 完成校验
// 没有上传文件
return NONE;
}
// 将上传文件 保存到服务器端
// 源文件 upload
// 目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + uploadFileName);
// 文件复制 使用commons-io包 提供 工具类
FileUtils.File(upload, destFile);
return NONE;
}
}
多文件上传
package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
* 支持多文件上传
*
* @author seawind
*
*/
public class MultiUploadAction extends ActionSupport {
// 接收多文件上传参数,提供数组接收就可以了
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;

public void setUpload(File[] upload) {
this.upload = upload;
}

public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}

public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}

@Override
public String execute() throws Exception {
for (int i = 0; i < upload.length; i++) {
// 循环完成上传
File srcFile = upload[i];
String filename = uploadFileName[i];

// 定义目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.File(srcFile, destFile);
}
return NONE;
}
}

E. java实现文件上传,代码尽量简洁~~~~~·

你说的2种方法都是很简单的,参考网上的资料都不难做出,用io流做更是基础中的基础,我说下smartupload好了,有的人是直接写在jsp上面,感觉比较乱,我一般都是写在action里面,打好jar包和配置后

SmartUpload mySmartUpload = new SmartUpload();

//如果是struts2.0或者webwork 则是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());

mySmartUpload.initialize(servlet.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(500000);
//如果上传任意文件不设置mySmartUpload.setAllowedFilesList(文件后缀名)就可以了
mySmartUpload.upload();
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(保存的地址 + file.getFileName(),
su.SAVE_PHYSICAL);

阅读全文

与java文件上传源码相关的资料

热点内容
pdf猫压缩 浏览:49
数据挖掘分类算法研究 浏览:101
河北航天发票认证服务器地址 浏览:727
阿里程序员绩效录入 浏览:382
eb蜜罐原理及源码 浏览:803
命令界面怎么调大小 浏览:890
公共测试服务器做什么的 浏览:746
湖南网络服务器云空间 浏览:365
做博客需要什么app 浏览:548
查看网站编程语言 浏览:302
我的世界服务器的密码怎么弄 浏览:644
电脑文件夹如何传输视频不压缩 浏览:145
安卓对单片机编程 浏览:728
被加密的txt 浏览:927
单片机汇编调用子程序 浏览:253
小米定位服务器地址 浏览:612
有什么好的学画app 浏览:882
5线4相步进电机单片机控制 浏览:293
linux重启php命令行 浏览:999
加密软件gif 浏览:761