
正文
java框架上传文件代码 java上传文件的几种方式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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);
相关问答
Q1: 使用java ssh框架 利用Uploadify做上传功能,后台action里该怎么写,请写出具体代码
Uploadify 是一个比较坑的东西,得用servlet, web.xml里配置 然后servlet接收
servlet
servlet-nameupload/servlet-name
servlet-classcom.action.upload/servlet-class
/servlet
servlet-mapping
servlet-nameupload/servlet-name
url-pattern/upload/FileUploadServlet/url-pattern
/servlet-mapping
servlet里代码
public void doGetAndPost(HttpServletRequest request,
HttpServletResponse response){
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
logger.error("后台添加图片,request设置编码符失败! {}",e.getMessage());
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
String imageName = null;
IteratorFileItem it = fileList.iterator();
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
Random r = new Random();
int rannum = (int) (r.nextDouble() * (9999 - 1000 + 1)) + 1000;
imageName=getNowStrDate() + rannum;
FileBean fileBean = new FileBean();
fileBean.setFileName(item.getName());
fileBean.setFileExtension(item.getName().substring(item.getName().indexOf(".")+1));
FileBean flbn;
try {
//文件服务器处理上传图片
flbn = FastDFSUtil.upload(fileBean, item.getInputStream());
} catch (IOException e) {
logger.info("添加图片,上传文件服务器失败!",e);
}
}
}
}
Q2: javaweb中实现文件上传完整代码(servlet里面dopost的代码)
structs
java框架上传文件代码的
jsp
页面文件上传表单,只要项目是SSHjava框架上传文件代码的就行java框架上传文件代码了
jsp:
s:form
action="add.do"
id="inputForm"
enctype="multipart/form-data"
td
s:file
name="upload"
cssClass="{required:true}"
contenteditable="false"/s:file
span
class="field_tipinfo"请选择文件/span
/td
/s:form
action:
private
File
upload;//上传的文件
....
public
String
add()
throws
Exception
{
//保存文件
save(upload);
}
...
Q3: 用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.copyFile(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.copyFile(srcFile, destFile);
}
return NONE;
}
}
关于java框架上传文件代码和java上传文件的几种方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





