快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

365体育平台bet_酒文化网进入



下载必要的两个包

common-fileupload

http://commons.a365体育平台betpache.org/fileupload/

下载获得

commons-fileupload-1.2.1.jar

common-io

http://commons.apache.org/io/

下载获得

commons-io-1.4.jar

因为我是应用的对照老版本的Struts。以是还碰到了些问题。

1、页面改动

先改动页面的FORM,首先在FORM中加入enctype="multipart/form-data"

同时在表单中增添type=file的元素

2、struts-config.xml的设置设置设备摆设摆设文件改动

去掉落了原本ACTION上面的ACTIONFORM设置设置设备摆设摆设

去掉落了parameter="method" 后面紧跟的name="VideoShowForm"

3、ACTION层代码的更改

因为加入了multipart/form-data标记,以是从request里面通报进来的其他参数生逝世拿不到。

以是自己从fileUpload里面的fileItems里面一个一个来取

写在ActionBase里面的基类措施

措施一,取到所有通报的参数的items

public List getFileItems(HttpServletRequest request) {

List fileItems = null;

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setSizeThreshold(4096);

// maximum size that will be stored in memory

// the location for saving data that is larger than getSizeThreshold()

FileUploadManager fileUploadManager = (FileUploadManager) getBean("fileUploadManager");

if (StringUtil.isNotBlank(fileUploadManager.getFileUploadTemp())) {

// 设置临时文件的寄放目录D:\\repository,或者可以不设置,不设置应该是系统默认的一个目录

//该目录必须已经存在

factory.setRepository(new File(fileUploadManager

.getFileUploadTemp()));

}

ServletFileUpload upload = new ServletFileUpload(factory);

// maximum size before a FileUploadException will be thrown

//上传文件的最大年夜大年夜小2,000,000,单位应该是byte,这里是2M

upload.setSizeMax(new Long(fileUploadManager.getFileUploadMax())

.longValue());

upload.setHeaderEncoding("UTF-8");

try {

fileItems = upload.parseRequest(request);

} catch (FileUploadException e) {

e.printStackTrace();

}

return fileItems;

}

措施二,从拿到的很多items中,获得对应的某个文本内容的值

public String getStringValue(HttpServletRequest request, String fieldName,

List fileItems) {

String fieldValue = "";

if (fileItems == null || fileItems.isEmpty()) {

return fieldValue;

}

Iterator iter = fileItems.iterator();

while (iter.hasNext()) {

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

if (item.isFormField()) {

if (fieldName.equalsIgnoreCase(item.getFieldName())) {

try {

fieldValue = item.getString("UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

}

}

return fieldValue;

}

措施三,从拿到的所有items中,获得对应的type=file的二进制的内容

public DiskFileItem getFileItem(HttpServletRequest request,

String itemN365体育平台betame, List fileItems) {

DiskFileItem fileItem = null;

Iterator iter = fileItems.iterator();

while (iter.hasNext()) {

fileItem = (DiskFileItem) iter.next();

if (fileItem.isFormField()) {

} else {

String name = fileItem.getFieldName();

long size = fileItem.getSize();

if (StringUtil.isBlank(name)

|| !itemName.equalsIgnoreCase(name) || size == 0) {

continue;

} else {

break;

}

}

}

return fileItem;

}

在ACTION中直接调用底层的ACTION基类

//获得所有的items

List items = this.getFileItems(request);

if (items != null && !items.isEmpty()) {

//获得一样平常的参数cityname

vsForm.setCityname(this.getStringValue(request, "cityname", items));

//获得二进制的参数uploadFile

DiskFileItem fileItem = this.getFileItem(request, "uploadFile", items);

if(fileItem != null){

FileUploadManager fileUploadManager = (FileUploadManager)getBean("fileUploadManager");

UserVO user = getCurrentUser(request);

String fileName = fileUploadManager.uploadFile(fileItem, user.getId(),vsForm.getVideoInputChannelId());

//通报二进制的item,用户名,文件名,调用fileUploadManager来完成上传文件

}

}

3、manager层新增的FileUploadManagerImpl.java:

package com.megaeyes.ipcamera.service.impl;

import java.io.File;

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

import com.megaeyes.ipcamera.service.iface.FileUploadManager;

import com.megaeyes.ipcamera.service.util.StringUtil;

public class FileUploadManagerImpl extends BaseManager implements

FileUploadManager {

public static final String FILE_UPLOAD_PATH = "D:\\\\upload";

//public static final String FILE_UPLOAD_TEMP = "D:\\\\repository";

public static final String FILE_UPLOAD_MAX = "2000000";

private String fileUploadTemp;

private String fileUploadPath;

private String fileUploadMax;

/**

* 上传文件

*fileItem fileUpload的二进制item

*userId用户ID当路径名

*文件名用上传的名字

*/

public String uploadFile(DiskFileItem fileItem, String userId) {

String fileName = "";

String path = "";

if (fileItem != null && StringUtil.isNotBl365体育平台betank(userId)) {

try {

path = this.getFileUploadPath() + "\\" + userId;

fileName = path + "\\" + getFileName(fileItem.getName());

File f = new File(path);

// 老师成目录

if (!f.exists()) {

f.mkdir();

}

if (f.exists()) {

// 目录存在开始拷贝文件

fileItem.write(new File(fileName));

}

} catch (Exception e) {

e.printStackTrace();

}

}

return fileName;

}

/**

* 上传文件

*fileItem fileUpload的二进制文件

*userId用户ID当路径名

*fileName 文件名字

*返回 文件路径

*/

public String uploadFile(DiskFileItem fileItem, String userId,

String fileName) {

String fullName = "";

String path = "";

if (fileItem != null && StringUtil.isNotBlank(userId)) {

try {

path = this.getFileUploadPath() + "\\" + userId;

fullName = path + "\\" + fileName

+ getFileTailName(fileItem.getName());

File f = new File(path);

if (!f.exists()) {

f.mkdir();

}

if (f.exists()) {

fileItem.write(new File(fullName));

}

} catch (Exception e) {

e.printStackTrace();

}

}

return fullName;

}

//取得365体育平台bet文件后缀名

private String getFileTailName(String fullName) {

String tailName = "";

if (StringUtil.isBlank(fullName)) {

return tailName;

}

int begin = fullName.lastIndexOf(".");

tailName = fullName.substring(begin, fullName.length());

return tailName;

}

//取得文件名字

private String getFileName(String fullName) {

String fileName = "";

if (StringUtil.isBlank(fullName)) {

return fileName;

}

int begin = fullName.lastIndexOf("\\");

fileName = fullName.substring(begin + 1, fullName.length());

return fileName;

}

public String getFileUploadTemp() {

return fileUploadTemp;

}

public void setFileUploadTemp(String fileUploadTemp) {

this.fileUploadTemp = fileUploadTemp;

}

public String getFileUploadPath() {

if (StringUtil.isBlank(fileUploadPath)) {

return FILE_UPLOAD_PATH;

}

return fileUploadPath;

}

public void setFileUploadPath(String fileUploadPath) {

this.fileUploadPath = fileUploadPath;

}

public String getFileUploadMax() {

if (StringUtil.isBlank(fileUploadMax)) {

return FILE_UPLOAD_MAX;

}

return fileUploadMax;

}

public void setFileUploadMax(String fileUploadMax) {

this.fileUploadMax = fileUploadMax;

}

public static void main(String[] args) {

FileUploadManagerImpl impl = new FileUploadManagerImpl();

String s = "c:\\\\test.jpg";

String test = impl.getFileName(s);

System.out.println(test);

}

}

该法度榜样在WINDOWS上测试经由过程,然则到linux上之后发明这个"\\"是不精确的,以是颠末同事的辅导

将这个符号整个改动成了

private final static Strin365体育平台betg FILE_SEPARATOR=File.separator;

FILE的这个属性可以根据操作系统来选择分隔符。可以适应各类操作系统。嘿嘿。

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: