FormFile类型如何转换为String

xuexixx 2009-12-25 02:05:42
uploadFfile.jsp

<……>
<html:form action="/uploadTest" enctype="multipart/form-data">
图片上传 : <html:file property="enterprise.logo" />
<html:submit />
<html:cancel />
</html:form>
<……>
================================
struts-config.xml

<……>
<struts-config>
<data-sources />
<form-beans >
<form-bean name="uploadForm" type="com.test.forms.UploadForm" />
</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
<action
name="uploadForm"
path="/uploadTest"
input="/uploadFfile.jsp"
scope="request"
type="com.test.actions.UploadAction" />

</action-mappings>
<……>
</struts-config>
====================================
UploadForm

<……>
public class UploadForm extends ActionForm {
private Enterprise enterprise = new Enterprise();

public void setEnterprise(Enterprise enterprise){
this.enterprise = enterprise;
}

public Enterprise getEnterprise(){
return enterprise;
}
}
<……>

==========================
下边是EnterpriseDto

public class EnterpriseDto{
private String logo; //logo地址
public void setLogo(String logo){
this.logo= logo;
}

public String getLogo(){
return logo;
}
}

=======================================
UploadAction

public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm )form;
FormFile logo = uploadForm.getEnterprise().getLogo();//在报的错是不能从String转化为FormFile

//我要在这得到Enterprise中的String logo保存在数据库中该如何写??急急急
}
}
...全文
654 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sean1203 2009-12-25
  • 打赏
  • 举报
回复
大哥
结贴。。。
xuexixx 2009-12-25
  • 打赏
  • 举报
回复
谢谢大家了,解决了
在UploadForm中添加了一个FormFile,在Action中这样写就可以了(红色部分)

UploadForm

<……>
public class UploadForm extends ActionForm {
private Enterprise enterprise = new Enterprise();
private FormFile logo;
<……setter getter>

public void setEnterprise(Enterprise enterprise){
this.enterprise = enterprise;
}

public Enterprise getEnterprise(){
return enterprise;
}
}

UploadAction

public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm )form;
String logoPath = uploadForm.getFileName();//取得文件名
//传过来的文件用流保存到指定的硬盘就好了 其他按步聚走就可了

}
}
crazylaa 2009-12-25
  • 打赏
  • 举报
回复
楼主一个个去比对。最下面那个类是用来存文件的。
jsp:
<html:form action="/chapter/chapter.do" method="POST"
enctype="multipart/form-data">

<td colspan="4">章节文件<font color="#FF0000">*</font>    <input type="file" name="formFile" contenteditable="false"
style="width:500"></td>

</html:form>


formbean:

private FormFile formFile = null;
public FormFile getFormFile() {
return formFile;
}

public void setFormFile(FormFile formFile) {
this.formFile = formFile;
}

action:

try {
ChapterObj chapterObj = chapterForm.getChapterObj();
SeriesObj seriesObj = chapterForm.getSeriesObj();
String filePath = seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/"
+ chapterObj.getChapterNo()
+ COMICCFG.STORE.CHAPTER_EXT;

chapterObj.setSeriesId(seriesObj.getSeriesId());
chapterObj.setFilePath(filePath);
try {
// 获取存储根路径
String storePath = GlobalInfo.getProperty(
COMICCFG.STORE.comic_content, COMICCFG.STORE.app,
COMICCFG.STORE.root_dir);
if (storePath == null || storePath.equals("")) {
throw new ComicException("取存储根路径失败!");
}
// 取上传后的文件
FormFile formFile = chapterForm.getFormFile();
if (formFile == null || formFile.getFileName().equals("")) {
throw new ComicException("取上传后的章节文件失败!");
}

String storeDir = storePath + seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/";
// 存文件
FileUpload.storeFile(formFile, storeDir, chapterObj
.getChapterNo());

// 取操作员ID
String operatorId = ((AdminUserForm) request.getSession()
.getAttribute("ADMIN_USER_INFORMATION"))
.getAdminUser().getUserId();

// 插入数据库
ChapterDAO.getInstance().insertChapter(chapterObj,
operatorId);
chapterObj.setChapterNo(String.valueOf(Integer
.parseInt(chapterObj.getChapterNo()) + 1));
} catch (ComicException e) {
if (e.getMessage().contains("ORA-00001")) {
throw new ComicException(" “章节序号-分辨率”重复!");
} else {
throw e;
}
} catch (Exception e) {
throw e;
}


FileUpload的存文件


/**
* 存储文件的操作类
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.struts.upload.FormFile;

public class FileUpload {

/**
* 将form接收到的文件存储到目标路径,文件名不变,文件类型后缀改为大写字母
*
* @param formFile
* action中form接受到的文件对象
* @param storeDir
* 目标路径
* @return 实际存储的文件全路径。
* @throws Exception
*/
public static String storeFile(FormFile formFile, String storeDir)
throws Exception {

String fileName = formFile.getFileName();
String fileExtension = "";
String filePath = "";
try {
if (fileName.length() == 0) {
throw new Exception("没有选择文件!");
}

// 取扩展名

if (fileName.lastIndexOf(".") > 0) {
fileExtension = fileName.substring(
fileName.lastIndexOf(".") + 1, fileName.length());
}

// 取文件名
String[] possibleSeparator = new String[] { "/", "\\" };
for (int i = 0; i < possibleSeparator.length; i++) {
String separator = possibleSeparator[i];
int index = fileName.lastIndexOf(separator);
if (index != -1) {
fileName = fileName.substring(index + separator.length());
break;
}
}

// 如果目录不存在,则创建目录
File f = new File(storeDir);
if (!f.exists()) {
f.mkdirs();
}
} catch (Exception e) {
throw e;
}
try {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
filePath = storeDir + fileName + "." + fileExtension.toLowerCase();
// 如果文件已存在,则删掉
File file = new File(filePath);
if (file.exists()) {
file.delete();
}

BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(formFile.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(new File(
filePath)));
byte[] bytes = new byte[2048];
int s = 0;
int success = 0;
while ((s = in.read(bytes)) != -1) {
out.write(bytes, 0, s);
success++;
}
if (success == 0) {
throw new Exception("文件读取错误!");
}
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
try {
out.close();
} catch (IOException e) {
// ignore
}
}

} catch (Exception ex) {
throw new ServletException("FileItem.write() fail", ex);
}
return filePath;
}

/**
* 将form接收到的文件存储到目标路径,文件名按输入的文件名,文件类型后缀改为大写字母
*
* @param formFile
* action中form接受到的文件对象
* @param storeDir
* 目标路径
* @param fileName
* 输入的文件名
* @return 实际存储的文件全路径。
* @throws Exception
*/
public static String storeFile(FormFile formFile, String storeDir, String fileName)
throws Exception {

String oldFileName = formFile.getFileName();
String fileExtension = "";
String filePath = "";

try {
if (fileName.length() == 0) {
throw new Exception("没有选择文件!");
}
// 取扩展名
if (oldFileName != null && oldFileName.lastIndexOf(".") > 0) {
fileExtension = oldFileName.substring(oldFileName
.lastIndexOf(".") + 1, oldFileName.length());
}

// 取文件名
String[] possibleSeparator = new String[] { "/", "\\" };
for (int i = 0; i < possibleSeparator.length; i++) {
String separator = possibleSeparator[i];
int index = fileName.lastIndexOf(separator);
if (index != -1) {
fileName = fileName.substring(index + separator.length());
break;
}
}

// 如果目录不存在,则创建目录
File f = new File(storeDir);
if (!f.exists()) {
f.mkdirs();
}
} catch (Exception e) {
throw e;
}

try {
filePath = storeDir + fileName + "." + fileExtension.toLowerCase();
// 如果文件已存在,则删掉
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(formFile.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(new File(
filePath)));
byte[] bytes = new byte[2048];
int s = 0;
int success = 0;
while ((s = in.read(bytes)) != -1) {
out.write(bytes, 0, s);
success++;
}
if (success == 0) {
throw new Exception("文件读取错误!");
}
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
try {
out.close();
} catch (IOException e) {
// ignore
}
}

} catch (Exception ex) {
throw new ServletException("FileItem.write() fail", ex);
}
return filePath;
}

}
crazylaa 2009-12-25
  • 打赏
  • 举报
回复
并且,你要在UploadForm 里面定义FormFile ff,并实现相应得get set方法。你得bean里面把它定义成String是不对的。
crazylaa 2009-12-25
  • 打赏
  • 举报
回复
最关键得丢了!!
<html:form action="/uploadTest" method="POST" enctype="multipart/form-data">
sean1203 2009-12-25
  • 打赏
  • 举报
回复
<html:file property="enterprise.logo" />
还是改成logo好了
sean1203 2009-12-25
  • 打赏
  • 举报
回复
FormFile logo = uploadForm.getEnterprise().getLogo();//在报的错是不能从String转化为FormFile
一步一步拿出来
别一下子拿这么多
logo=uploadForm.getFile();
getFile是取得页面那个file控件里面的文件

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧