/Files/kokosang/html5拖拽上传.zip 前几天想做个给安卓app升级做个上传页面,然后从网上down了个页面下面(主要是嫌弃自己页面整的太丑,见不了人),然后就一直在整后台取值的办法
各种百度,值取出来了,但是,悲催的是总是乱码,崩溃了,大神看了后,鄙视一番,给我整了下,简直就是重写了
贴出来,先放张页面效果
赏心悦目的后台来咯
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import com.puff.framework.annotation.Controller;
import com.puff.web.mvc.PuffContext;
import com.puff.web.view.View;
@Controller("/upload/android")
public class AndroidVersionUploadController {
private static final Logger logger = Logger.getLogger(UserImageController.class);
private static final String FILE_PATH = "C:/Users/Administrator/Desktop";
private static final String FILE_APK_NAME = "/nn.apk";
private static final String FILE_VER_NAME = "/ver.txt";
/**
* 版本上传
* @return
* @throws Exception
*/
public View update() throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest( PuffContext.getRequest());
String ver = null;
String info = null;
String isFoucs = null;
for(Object object : items) {
FileItem fileItem = (FileItem)object;
if(fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString("utf-8");
if("ver".equals(name)){
ver = value;
}else if("content".equals(name)){
info = value;
}else if("radio".equals(name)){
isFoucs = value;
}
} else {
saveFileInfo(fileItem);
}
}
saveContentInfo(ver, info, isFoucs);
return null;
}
private void saveFileInfo(FileItem fileItem) {
InputStream is = null;
OutputStream os = null;
try {
File file = new File(FILE_PATH);
if(file.exists()) {
file.mkdirs();
}
is = fileItem.getInputStream();
os = new FileOutputStream(FILE_PATH + FILE_APK_NAME);
int len = 0;
byte[] buffer = new byte[8 * 1024];
while ((len = is.read(buffer, 0, 8 * 1024)) != -1) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeOutputStream(os);
closeInputStream(is);
}
}
public void saveContentInfo(String ver, String info, String isFoucs) {
BufferedWriter br = null;
try {
File file = new File(FILE_PATH);
if(file.exists()) {
file.mkdirs();
}
br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FILE_PATH + FILE_VER_NAME), "utf-8"));
br.write("ver:" + ver + "\r\n"); //版本号
br.write("update:" + isFoucs+ "\r\n"); //是否强制更新
br.write("content:" + info ); //版本升级内容
br.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
br = null;
}
}
}
private void closeOutputStream(OutputStream os) {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
os = null;
}
}
}
private void closeInputStream(InputStream is) {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
is = null;
}
}
}
}
posted on 2015-08-24 10:35
藤本蔷薇 阅读(258)
评论(0) 编辑 收藏