十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
// 这是我写的一个方法,里面只需要传两个参数就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中可以用struts2实现多个文件同时上传代码,功能代码如下:
1、upload.jsp页面(选择上传文件)
form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data"
文件标题:input type="text" name="title"/br/
选择文件-:input type="file" name="upload"/br/
选择文件二:input type="file" name="upload"/br/
选择文件三:input type="file" name="upload"/br/
input type="submit" value="upload"/
/form
2、action代码如下:
//对应的Action依次遍历所有文件域,然后生成对应的输入文件流,输出文件流在指定的服务器保存路径中添加对应的输出文件流保存文件。同时动态指定服务器上文件的保存路径。
package com.inspur.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadFileName;
private String[] uploadContentType;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload()throws Exception{
File[] files=this.getUpload();
for(int i=0;ifiles.length;i++){
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName()[i]);
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(files[i]);
int len=0;
while((len=fis.read(buffer))0){
fos.write(buffer,0,len);
}
}
return SUCCESS;
}
}
3、success.jsp页面代码如下(上传成功界面显示所有上传的图片)
文件标题:s:property value="title"/br/
第一个图片:img alt="first" src="s:property value="'upload/'+uploadFileName[0]"/"/br/
第二个图片:img alt="second" src="s:property value="'upload/'+uploadFileName[1]"/"/br/
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;
}
}